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...
The current answers may work but I find they make things too complicated instead of using logrotate in a way where it already handles things very well. You don't need to specify commands manually if logrotate already handles this. You don't need complicated configuration if you just address the initial problem of catalina doing the logrotation and let logrotate handle it.
It looks like the files are already being rotated as illustrated by the date in the logfile names in the question:
catalina.2012-01-20.log
catalina.2012-01-21.log
catalina.2012-01-22.log
So, which component already does this, is it configured in /etc/logrotate.d/tomcat*? What is the content of this file on your system currently? If I remember correctly, catalina may already doing this itself: Creating a new logfile every day and moving older files to their new location. If you want logrotate to handle this instead, you may want to change the default behaviour of tomcat / catalina, as described in this answer first.
After that, catalina should always write to catalina.log
.
Then you can put the rotation / compression / deletion entirely into the responsibility of logrotate.
If you want to compress older files, you can add compress to the logrotate configuration:
For example, you have:
/somepath/catalina.log {
# truncate file in place
copytruncate
# rotate daily
daily
# keep max 7, will delete after that
rotate 7
# when rotating create file with date, e.g. catalina.log-20200304
dateext
# compress
compress
# manpage: "Postpone compression of the previous log file
# to the next rotation cycle.
# This only has effect when used in combination with compress."
delaycompress
# change this to correct owner and permissions of logfile
create 750 tomcat6 tomcat6
# do not generate an error if logfile is missing
missingok
# do not rotate if the file is empty
notifempty
}
What you should get as a result:
catalina.log
catalina.log-20200304
catalina.log-20200303.gz
...
If things are not working:
A common error is that people run logrotate manually and then get puzzled if nothing is changed. Logrotate will only perform the operations it has been configured to perform. If you specify a daily rotate, it will not rotate again even if you run it as often as you want, unless you use -f. It will use the status file to keep track. You can use -v and logrotate will report what it is doing and why.
Resources:
/path/to/logs/*.log { missingok compress rotate 7 }
this type of thing doesn't work normally because as others point out tomcat has its own log rotation. You can either use a simple cron to delete old files or turn off rotation on the access log valve. By turning off log rotation (and possible changing the filename patter), the above logrotate and other similar configs will work fine.
The bottom line is you should use logrotate or the built in log rotation in tomcat but not both at the same time.
Something like this in /etc/cron.d/rotate_tomcat_logs:
# delete every log file over 100 days old, and compress every log file over 1 day old.
00 1 * * * root ( find /opt/tomcat/logs -name \*log\* -name \*.gz -mtime +100 -exec rm -f {} \; >/dev/null 2>&1 )
05 1 * * * root ( find /opt/tomcat/logs -name \*log\* ! -name \*.gz -mtime +1 -exec gzip {} \; >/dev/null 2>&1 )
Also, you can add crons instead hardcode logrotate.
1 0 * * * /usr/bin/find /var/log/tomcat/ -mtime +30 -delete
2 0 * * * /usr/bin/find /var/log/tomcat/ -mtime +1 -exec gzip -q {} \;
In this way, you will delete the logs older than 30 days, and zip the logs older than 1.