I am trying to configure logrotate in RHEL for tomcat6 logs. Currently, logrotate works fine for catalina.out log, it is rotated and compressed properly.
The problem
Well, I was not fully satisfied with any of the answers, even though the ones stating that the logrotate
doesn't support this (i. e. just to remove files rotated by some other application) scenario are surely correct (raise a feature request on that tool, maybe?).
So, I would like to share an alternative approach I've come to. Unlike the "find /path/to/logs -mtime +7 -delete
" solution, this one won't remove all the old logs after a specified period of time. So here it comes, an example one-liner bash command which leaves just N last logs on the disk (whenever it is run):
for f in `ls -1r | grep -E "^catalina\.[0-9]{4}-[0-9]{2}-[0-9]{2}\.log$" | tail -n +$((N+1))`; do rm $f; done
Finally, to cover the topic completely, the last alternative solution is not to rotate the log files (e. g. use rotatable=false
in case of Tomcat - see its docs) and use the logrotate
as usually, but don't forget to use it with the 'copytruncate' option.
Suggestions are welcome...