{"id":1743,"date":"2012-12-03T12:58:07","date_gmt":"2012-12-03T04:58:07","guid":{"rendered":"http:\/\/rmohan.com\/?p=1743"},"modified":"2012-12-03T13:09:28","modified_gmt":"2012-12-03T05:09:28","slug":"tomcat-catalina-out-rotate","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1743","title":{"rendered":"Tomcat catalina.out Rotate"},"content":{"rendered":"<p>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.<\/p>\n<h3>How to automatically rotate catalina.out daily or when it becomes bigger than 5M<\/h3>\n<p><strong>1. Create this file<\/strong><\/p>\n<pre>\/etc\/logrotate.d\/tomcat<\/pre>\n<p><strong>2. Copy the following contents into the above file<\/strong><\/p>\n<pre>\/var\/log\/tomcat\/catalina.out {\r\n copytruncate\r\n daily\r\n rotate 7\r\n compress\r\n missingok\r\n size 5M\r\n}<\/pre>\n<p>About the above configuration:<\/p>\n<ul>\n<li>Make sure that the path\u00a0<strong>\/var\/log\/tomcat\/catalina.out<\/strong>\u00a0above is adjusted to point to your tomcat\u2019s catalina.out<\/li>\n<li><strong>daily\u00a0<strong>&#8211;\u00a0<\/strong><\/strong>rotates the catalina.out\u00a0<strong>daily<\/strong><\/li>\n<li><strong>rotate<\/strong>\u00a0\u2013 keeps at most\u00a0<strong>7<\/strong>\u00a0log files<\/li>\n<li><strong>compress \u2013\u00a0<\/strong>compresses<strong>\u00a0<\/strong>the rotated files<\/li>\n<li><strong>size<\/strong>\u00a0\u2013 rotates if the size of catalina.out is bigger than\u00a0<strong>5M<\/strong><\/li>\n<li><strong>copytruncate<\/strong>\u00a0\u2013 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.\u00a0<strong>Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost<\/strong>. When this option is used, the create option will have no effect, as the old log file stays in place.<\/li>\n<\/ul>\n<p>You don\u2019t need to do anything else.<\/p>\n<h3>How it works<\/h3>\n<ol>\n<li>Every night the cron daemon runs jobs listed in the\u00a0<strong>\/etc\/cron.daily\/<\/strong>\u00a0directory<\/li>\n<li>This triggers the\u00a0<strong>\/etc\/cron.daily\/logrotate<\/strong>\u00a0file which is generally shipped with linux installations. It runs the command \u201c<strong>\/usr\/sbin\/logrotate \/etc\/logrotate.conf<\/strong>\u201c<\/li>\n<li>The\u00a0<strong>\/etc\/logrotate.conf<strong>\u00a0<\/strong><\/strong>includes all scripts in the<strong><strong>\u00a0<\/strong><strong>\/etc\/logrotate.d\/\u00a0<\/strong><\/strong>directory.<\/li>\n<li>This triggers the\u00a0<strong>\/etc\/logrotate.d\/tomcat<\/strong>\u00a0file that you wrote in the previous step.<\/li>\n<\/ol>\n<h3>Run logrotate manually<\/h3>\n<p>Run the following command to run the cron job manually<\/p>\n<pre>\/usr\/sbin\/logrotate \/etc\/logrotate.conf<\/pre>\n<h3>Is it completely safe?<\/h3>\n<p>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.<\/p>\n<h3>More logrotate options<\/h3>\n<p>To see all logrotate options on your system, see the manual:<\/p>\n<pre>man logrotate<\/pre>\n<p><strong>quick tip: rotating tomcat logs via cronolog<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.compile cronolog (http:\/\/cronolog.org) and install it on \/usr\/local\/sbin<br \/>2. backup orig \/usr\/local\/apache-tomcat-5.5.20\/bin\/catalina.sh on \/root<br \/>3. edit lines on catalina.sh from<br \/>org.apache.catalina.startup.Bootstrap \u201c$@\u201d start \\<br \/>&gt;&gt; \u201c$CATALINA_BASE\u201d\/logs\/catalina.out 2&gt;&amp;1 &amp;<\/p>\n<p>to<\/p>\n<p>org.apache.catalina.startup.Bootstrap \u201c$@\u201d start 2&gt;&amp;1 \\<br \/>| \/usr\/local\/sbin\/cronolog \u201c$CATALINA_BASE\u201d\/logs\/catalina.out.%Y-%m-%d &gt;&gt; \/dev\/null &amp;<\/p>\n<p>4. Removed the line<br \/>touch \u201c$CATALINA_BASE\u201d\/logs\/catalina.out<\/p>\n<p>5. Restart web service<br \/>#\/usr\/local\/tomcat\/bin\/shutdown.sh<br \/># service httpd stop<br \/># \/usr\/local\/tomcat\/bin\/startup.sh<br \/>#service httpd start<\/p>\n<p>TESTING<\/p>\n<p>1. Check tomcat logs to see generated Catalina.out per day<br \/># ls -la \/usr\/local\/tomcat\/logs<\/p>\n<p>-rw-r\u2013r\u2013 1 root root 65607 Nov 6 14:55 catalina.out.2007-11-06<\/p>\n<p>2. browse ww2.freelinuxtutorials.com and login using test account to see if tomcat is working<\/p>\n<p>FALLBACK<\/p>\n<p>1. copy original Catalina.sh file to the location<br \/>2. cp \/root\/catalina.sh \/usr\/local\/apache-tomcat-5.5.20\/bin\/<br \/>3. restart web<br \/>#\/usr\/local\/tomcat\/bin\/shutdown.sh<br \/># \/usr\/local\/tomcat\/bin\/startup.sh<br \/>#service httpd restart<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n<p> How to automatically rotate catalina.out daily or when it becomes bigger than 5M <\/p>\n<p>1. Create [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1743"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1743"}],"version-history":[{"count":4,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1743\/revisions"}],"predecessor-version":[{"id":1750,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1743\/revisions\/1750"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}