Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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')
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.