Vineet Manohar is CTO / Co-Founder of online Car Search Engine, iSeeCars.com. Vineet is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

How to Rotate Tomcat catalina.out

03.25.2010
| 119164 views |
  • submit to reddit

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

  1. Every night the cron daemon runs jobs listed in the /etc/cron.daily/ directory
  2. 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
  3. The /etc/logrotate.conf includes all scripts in the /etc/logrotate.d/ directory.
  4. 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  

From http://www.vineetmanohar.com

Published at DZone with permission of Vineet Manohar, author and DZone MVB.

(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

Hasn't Tomcat make use of Log4J by the way?

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:

copytruncate
Truncate the original log file in place after creating a copy,
instead of moving the old log file and optionally creating a new
one, It can be used when some program can not be told to close
its logfile and thus might continue writing (appending) to the
previous log file forever. Note that there is a very small time
slice between copying the file and truncating it, so some log-
ging data might be lost. When this option is used, the create
option will have no effect, as the old log file stays in place.

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.

 

    "$_RUNJAVA" $JAVA_OPTS "$LOGGING_CONFIG" $CATALINA_OPTS \
-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
-Djava.security.manager \
-Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
-Dcatalina.base="$CATALINA_BASE" \
-Dcatalina.home="$CATALINA_HOME" \
-Djava.io.tmpdir="$CATALINA_TMPDIR" \
org.apache.catalina.startup.Bootstrap "$@" start \
2>&1 | tee -a "$CATALINA_BASE"/logs/catalina.out > /dev/null & #This Bit

What this does is to replace the >> operator with the 'tee' command which behaves correctly on all shells.

Hope this helps.

 

 

 

Bhaskar Selvaraj replied on Tue, 2013/11/12 - 5:34am

Where does the rotated log files get placed.Is it on the same directory where the original file resides?


Regards,

Bhaskar

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.