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
  • 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

Related posts:

  1. How to automatically recover Tomcat from crashes
  2. Getting started with Nexus Maven Repo Manager
  3. Cache Java webapps with Squid Reverse Proxy

20 comments to How to rotate Tomcat catalina.out

  • John Smith

    I don’t agree that this will work. catalina.out is open all the time so after the logrotate happens once, nothing else will be logged because you won’t have a handle to the inode that standard out is writing to.

    The only way to fix this is to either restart tomcat or modify catalina.sh so it doesn’t do a > redirect.

  • vineet

    It does work for me.

    See the documentation of “copytruncate” option of logrotate:
    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.

  • Amir Laher

    I never knew about the copytruncate option. Seems nicer than modifying catalina.sh + logging configs.
    Very powerful, thanks

  • michael

    Why is the log going to catalina.out? We should use log4j or some logging api to pipe it to a log file instead of using stdout.

  • hi, i couldn’t find logrotate in my OS. i use fedora-linux. where to download this app ?

  • vineet

    Install the logrotate rpm using the following command:
    1) login as root
    2) rpm -Uvh ‘rpm path or rpm URL’

  • A simple solution for a great problem: thanks vineet!!!

  • Shantanu Pavgi

    Vineet, Thanks for sharing your conf. Any specific reason for using logrotate and not log4J? Just curious to know Pros/Cons of both configurations if you have tried both. I found log4J DailyRollingFileAppender and RollingFileAppender pretty useful. One drawback being of DailyRollingFileAppender being no out of the box option to specify max. days to keep log files (Log4J ver. 1.2). I guess this can be handled using cron and find commands.

  • Daniel Mikusa

    Interesting that you are getting crashes at 2GB. Sounds like it may not be a Tomcat issue and it may be an issue with large file support in your Linux distribution.

    http://linuxmafia.com/faq/VALinux-kb/2gb-filesize-limit.html

  • Carlos

    What will happen if I mention both daily/weekly/monthly and size parameters both? I think only later config parameter works and first one is ignored. For example if you log file doesn’t get bigger than 5M in a day, then it won’t be rotated even though you have specified DAILY option. So it’s ORring between DAILY and SIZE. Please correct me if I am wrong.

  • Ajith

    Am getting this error lines must begin with a keyword or a filename (possibly in double quotes) on the first line one of tomcat logrotate conf file. How to fix this?

  • Nice Thomas

    Thanks
    its working fine

  • Thanks Vineet, the solution i’ve been looking for. Tested in my production, works like a charm.

  • Anon

    Tried it running it manually and nothing happened, no error message. Maybe tonight it will rotate

  • Ghaleb

    Thank you very much for your help, i tried it manually but nothing happened.
    should i wait for auto rotate or there is something wrong?
    @Anon did it works or you find something wrong? your advice please

  • Tomek

    thank you, that was helpful!

  • wilbert

    Thanks,
    its working fine

  • girish

    Hi Vineet,

    I have just followed all the steps as you have mentioned but its not working for me.

    Any Suggestion would be appreciated.

    Server Info: Red Hat Enterprise Linux Server release 6.1

    Tomcat used: apache-tomcat-7.0.12

    Thanks
    Girish

  • Narayan

    Hey Friends,

    I tried running manually, couldn’t find any difference in file system. should i be waiting for automatic rotate? or did i do anything wrong?

    Its been very painful work in my UAT to restart server every time when it exceeds size limit.

    appreciated your help

    -Narayan

  • sahar

    i tried it manually but nothing happened.
    should i wait for auto rotate or there is something wrong?
    please answer me, thanks :)

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=""> <strike> <strong>

Get Adobe Flash playerPlugin by wpburn.com wordpress themes