3

I have a service that automatically creates a log file with a timestamp in the filename on startup. Thus, I don't need logrotate to rename/copy/create files, but what I do want is that logrotate shall keep just the three newest of those files (and optionally compress them). Can I do that somehow?

3 Answers 3

4

I'm not sure you can do this by using logrotate. Can you run the following as a daily cron job:

rm $(ls -t | sed -e '1,3d')
1

I do not think that it can be done exactly as you want. In case you want to still use logrotate first you need to specify how often logrotate will rotate your log (daily/weekly/monthly/yearly). You can set it like this:

# rotate log files daily
daily

# Log files are rotated count times before being removed or mailed to the address
# specified in a mail directive. If count is 0, old versions are removed rather than 
# rotated.
rotate 3

# Old versions of log files are compressed with gzip(1) by default.
compress

But as @quanta wrote it can't be probably done with logrotate. You will either use some similar settings as I wrote above, or you will need to use probably other tool.

1
  • And maybe with "copyontruncate"
    – ppuschmann
    Feb 17, 2021 at 16:42
0

I was attempting to find a similar logrotate solution for ProxySQL's log files, which look like:

/var/lib/proxysql/queries.log.00000377
/var/lib/proxysql/queries.log.00000378
/var/lib/proxysql/queries.log.00000379
/var/lib/proxysql/queries.log.00000380
/var/lib/proxysql/queries.log.00000381

In the end I gave up on logrotate and just went with a simple cron solution:

# Delete old query log files 60 days old
0 4 * * * find /var/lib/proxysql -name 'queries.log.*' -mtime +60 -delete

# compress 5 day old query log files
30 4 * * * find /var/lib/proxysql -name 'queries.log.????????' -mtime +5 -exec gzip {} \;
2
  • ...and then you delete a file with open file-handlers... boom I don't like this approach.
    – ppuschmann
    Feb 17, 2021 at 16:41
  • Log files newer than 5 days old are not touched. By that time, the daemon writing the files has closed file descriptors and they should be all gone. (I did also confirm this with lsof on a running ProxySQL instance, the most recent log file is the only one open)
    – Cal
    Feb 18, 2021 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .