I'm rotating log files with logrotate
with copytruncate
, and I need to run a script with the new filename as argument. I understand I can use $1
within postrotate
, but that gives me the name of the rotated file – not the one with the date extension.
So basically I have:
/var/log/myapp/server.log
When I run this logrotate config:
/var/log/myapp/server.log {
copytruncate
rotate 1008
dateext
dateformat -%s
create 660 syslog syslog
postrotate
python3 /opt/myapp/test.py $1 > /dev/null
endscript
}
I get
/var/log/myapp/server.log <-- (now empty)
/var/log/myapp/server.log-1536844088
Can I run a script on /var/log/myapp/server.log-1536844088
in postrotate somehow?
Best Answer
It's my understanding that logrotate will pass the name of the log file to the script in
postrotate
; you could use the script and the first parameter and the knowledge that logrotate has created rotated files using the-%s
dateformat to generate a list of those files:This is my first python script, so it may not be idiomatic, but hopefully shows the idea. The fnmatch() function uses a glob that should cover you for several years (until late November of 2286), when 9,999,999,999 seconds will roll over to 10,000,000,000. You could "widen" the glob so that it "just" requires "10 digits and then anything":