logrotate: keep N newest files, but don't rename

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?


Solution 1:

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')

Solution 2:

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.