{"id":680,"date":"2012-07-11T11:13:42","date_gmt":"2012-07-11T03:13:42","guid":{"rendered":"http:\/\/rmohan.com\/?p=680"},"modified":"2012-07-11T14:18:40","modified_gmt":"2012-07-11T06:18:40","slug":"logrotate","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=680","title":{"rendered":"Logrotate"},"content":{"rendered":"<p><strong>Logrotate is the default and easiest log management tool around. It is shipped by default with most of the major Linux distributions. Logrotate can help you to rotate logs (in other words, it can create a separate log file per day\/week\/month\/year or on the basis of size of log file). It can compress the older log files. It can run custom scripts after rotation. It can rename the log to reflect the date.<br \/>\nLogrotate scripts goes to \/etc\/logrotate.d\/. Let us see some examples to understand it better. Here we\u2019ll rotate \/var\/log\/anyapp.log<\/strong><\/p>\n<p>1. Rotate logs daily:<br \/>\n$ cat \/etc\/logrotate.d\/anyapp<br \/>\n\/var\/log\/anyapp.log {<br \/>\ndaily<br \/>\nrotate 7<br \/>\n}<\/p>\n<p>The logrotate script above will rotate \/var\/log\/anyapp.log everyday and it\u2019ll keep the last 7 rotated log Files To Other Machines On The Local Network [Linux, Windows] \u00bb href= \u00bbhttp:\/\/www.adminreseau.fr\/nitroshare-easily-send-files-to-other-machines-on-the-local-network-linux-windows\/ \u00bb&gt;files. Instead of daily you can use monthly or weekly also.<\/p>\n<p>2. Compress the rotated logs:<br \/>\n$ cat \/etc\/logrotate.d\/anyapp<br \/>\n\/var\/log\/anyapp.log {<br \/>\ndaily<br \/>\nrotate 7<br \/>\ncompress<br \/>\n}<\/p>\n<p>Now you\u2019ll find that logrotate is also compressing the rotated files. This is really a big life saver if you want to save some disk space which is a very common use case specially in VPS or cloud environment.<br \/>\nBy default logrotate does a gzip compression. You can alter this behavior by using compresscmd. For example \u00ab compresscmd \/bin\/bzip2 \u00bb will get you bzip2 compression.<\/p>\n<p>3. Compress in the next cycle:<br \/>\n$ cat \/etc\/logrotate.d\/anyapp<br \/>\n\/var\/log\/anyapp.log {<br \/>\ndaily<br \/>\nrotate 7<br \/>\ncompress<br \/>\ndelaycompress<br \/>\n}<\/p>\n<p>This is useful in case it is not possible to immediately compress the file. This happens when the process keeps on writing to the old file even after the rotation. If the last line sounded strange to you then you Unity 2D Might Go Away, Released With Improved UI, More [PPA] \u00bb href= \u00bbhttp:\/\/www.adminreseau.fr\/audacious-3-3-alpha-1-released-with-improved-ui-more-ppa\/ \u00bb&gt;More [Ubuntu 12.10] \u00bb href= \u00bbhttp:\/\/www.adminreseau.fr\/uds-q-news-unity-2d-might-go-away-more-ubuntu-12-10\/ \u00bb&gt;might want to read about inodes. Also note that \u00ab delaycompress \u00bb will work only if \u00ab compress \u00bb is included in the script.<\/p>\n<p>4. Compressing the copy of the log:<br \/>\n$ cat \/etc\/logrotate.d\/anyapp<br \/>\n\/var\/log\/anyapp.log {<br \/>\ndaily<br \/>\nrotate 7<br \/>\ncompress<br \/>\ndelaycompress<br \/>\ncopytruncate<br \/>\n}<\/p>\n<p>Copytruncate comes handy in the situation where process writes to the inode of the log and rotating the log might cause process to go defunct or stop logging or a bunch of other issues. Copytruncate copies the log and the further processing is done on the copy. It also truncates the original file to zero bytes. Therefore the inode of the file is unchanged and process keeps on writing to the log file as if nothing has happened.<\/p>\n<p>5. Don\u2019t rotate empty log and don\u2019t give error if there is no log:<br \/>\n$ cat \/etc\/logrotate.d\/anyapp<br \/>\n\/var\/log\/anyapp.log {<br \/>\ndaily<br \/>\nrotate 7<br \/>\ncompress<br \/>\ndelaycompress<br \/>\ncopytruncate<br \/>\nnotifempty<br \/>\nmissingok<br \/>\n}<\/p>\n<p>Self explanatory. Both \u00ab notifempty \u00bb and \u00ab missingok \u00bb has opposite twins named \u00ab ifempty \u00bb and \u00ab nomissingok \u00bb which are the defaults for logrotate.<\/p>\n<p>6. Execute custom script before and\/or after logrotation:<br \/>\n$ cat \/etc\/logrotate.d\/anyapp<br \/>\n\/var\/log\/anyapp.log {<br \/>\ndaily<br \/>\nrotate 7<br \/>\nprerotate<br \/>\n\/bin\/myprescript.sh<br \/>\nendscript<br \/>\npostscript<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logrotate is the default and easiest log management tool around. It is shipped by default with most of the major Linux distributions. Logrotate can help you to rotate logs (in other words, it can create a separate log file per day\/week\/month\/year or on the basis of size of log file). It can compress the older [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/680"}],"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=680"}],"version-history":[{"count":5,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/680\/revisions"}],"predecessor-version":[{"id":682,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/680\/revisions\/682"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}