May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Tomcat catalina.out Rotate

tail -f 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
  • copytruncate – truncates 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 logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

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

Is it completely safe?

See the description of copytruncate method above. There is a slight chance of a small amount of logging data loss between copy and truncate steps, usually it is acceptable but sometimes its not.

More logrotate options

To see all logrotate options on your system, see the manual:

man logrotate

quick tip: rotating tomcat logs via cronolog

 

1.compile cronolog (http://cronolog.org) and install it on /usr/local/sbin
2. backup orig /usr/local/apache-tomcat-5.5.20/bin/catalina.sh on /root
3. edit lines on catalina.sh from
org.apache.catalina.startup.Bootstrap “$@” start \
>> “$CATALINA_BASE”/logs/catalina.out 2>&1 &

to

org.apache.catalina.startup.Bootstrap “$@” start 2>&1 \
| /usr/local/sbin/cronolog “$CATALINA_BASE”/logs/catalina.out.%Y-%m-%d >> /dev/null &

4. Removed the line
touch “$CATALINA_BASE”/logs/catalina.out

5. Restart web service
#/usr/local/tomcat/bin/shutdown.sh
# service httpd stop
# /usr/local/tomcat/bin/startup.sh
#service httpd start

TESTING

1. Check tomcat logs to see generated Catalina.out per day
# ls -la /usr/local/tomcat/logs

-rw-r–r– 1 root root 65607 Nov 6 14:55 catalina.out.2007-11-06

2. browse ww2.freelinuxtutorials.com and login using test account to see if tomcat is working

FALLBACK

1. copy original Catalina.sh file to the location
2. cp /root/catalina.sh /usr/local/apache-tomcat-5.5.20/bin/
3. restart web
#/usr/local/tomcat/bin/shutdown.sh
# /usr/local/tomcat/bin/startup.sh
#service httpd restart

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>