How to Rotate Tomcat catalina.out
If catalina.out becomes 2GB in size, tomcat crashes and fails to start without any error message. To avoid this scenario you should rotate catalina.out frequently. This article describes how to setup auto rotation of catalina.out on a linux/unix machine.
How to automatically rotate catalina.out daily or when it becomes bigger than 5M
1. Create this file
/etc/logrotate.d/tomcat
2. Copy the following contents into the above file
/var/log/tomcat/catalina.out {
copytruncate
daily
rotate 7
compress
missingok
size 5M
}
About the above configuration:
- Make sure that the path /var/log/tomcat/catalina.out above is adjusted to point to your tomcat’s catalina.out
- daily - rotates the catalina.out daily
- rotate – keeps at most 7 log files
- compress – compresses the rotated files
- size – rotates if the size of catalina.out is bigger than 5M
You don’t need to do anything else.
How it works
- Every night the cron daemon runs jobs listed in the /etc/cron.daily/ directory
- This triggers the /etc/cron.daily/logrotate file which is generally shipped with linux installations. It runs the command “/usr/sbin/logrotate /etc/logrotate.conf“
- The /etc/logrotate.conf includes all scripts in the /etc/logrotate.d/ directory.
- This triggers the /etc/logrotate.d/tomcat file that you wrote in the previous step.
Run logrotate manually
Run the following command to run the cron job manually
/usr/sbin/logrotate /etc/logrotate.conf
More logrotate options
To see all logrotate options on your system, see the manual:
man logrotate
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Lukas Zapletal replied on Thu, 2010/03/25 - 6:44am
Is this library able to logrotate files itself?
Do I miss something? Thanks
ps - nice traffic anyway ;-)
Kevin Jordan replied on Thu, 2010/03/25 - 9:10am
in response to:
Lukas Zapletal
It uses Log4j for everything else, but catalina.out gets a copy of everything printed to stdout and stderr.
But, unless it's been changed in version 6 of Tomcat, I always thought that the file was never recreated if you moved it out and that the service had to be restarted for it to get output in the file again.
You can also do a swallowOutput as shown in the context docs here at http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Standard%20Implementation though which will just leave you with start and stop messages.
Vineet Manohar replied on Thu, 2010/03/25 - 10:28am
The original file is never moved, it is truncated inplace. See the documentation of logrotate's copytruncate option:
J Rey replied on Thu, 2010/03/25 - 10:40am
Can this cron job perform the operations on the log file while tomcat is running, or does this need to be part of a restart script? Can the script be used to rotate other log files in addition to catalina.out?
Thanks for the great information!
Vineet Manohar replied on Thu, 2010/03/25 - 12:12pm
in response to:
J Rey
You can use it while tomcat is running. It truncates the catalina.out in-place after creating a copy.
You can use logrotate to rotate other files as well. Create a copy the file "/etc/logrotate.d/tomcat" in the same directory and change the first line to point it to the log file that you want to rotate.
Peter Ox replied on Wed, 2010/05/05 - 7:49pm
For those of you using Solaris, and possibly other *nix flavours, the above will still not work due to limitations in the bourne shell. See http://www.in-ulm.de/~mascheck/bourne/common.html and search for O_APPEND.
Basically, the >> operator in catalina.sh when run using the sh process doesn't allow for truncating of a file. After logrotate does its stuff, the next time catalina.out is written to it will continue to write to the old end of the file, causing a spare file will a lot of nulls at the beginning.
To get around this I changed catalina.sh as follows.
What this does is to replace the >> operator with the 'tee' command which behaves correctly on all shells.
Hope this helps.