{"id":182,"date":"2017-03-03T13:14:52","date_gmt":"2017-03-03T12:14:52","guid":{"rendered":"http:\/\/owncloud.gonscak.sk\/?p=182"},"modified":"2019-03-11T10:11:06","modified_gmt":"2019-03-11T09:11:06","slug":"setting-up-logrotate-on-centos-7","status":"publish","type":"post","link":"https:\/\/www.gonscak.sk\/?p=182","title":{"rendered":"Setting up logrotate on Centos 7"},"content":{"rendered":"<p>Yesterday, I met with problem of low capacity \/var\/log\/ partition. Some logs were too big and logrotate is the perfect tool to handle this problem. It is a software designed for reduce amount of space for every log file we have. And it can be done with some ways.<br \/>\nLogrotate Description: logrotate\u00a0 is\u00a0 designed\u00a0 to\u00a0 ease\u00a0 administration of systems that generate large numbers of log files.\u00a0 It allows automatic rotation, compression, removal, and mailing of log files.\u00a0 Each log file may be handled daily, weekly, monthly, or when\u00a0 it\u00a0 grows too large.<br \/>\nNormally,\u00a0 logrotate\u00a0 is\u00a0 run as a daily cron job.\u00a0 It will not modify a log multiple times in one day So in few words, logrotate is reducing space usage on disk by log files.<\/p>\n<h4>Logrotate configuration<\/h4>\n<p>Configuration of logrotate is made in one main file: \/etc\/logrotate.conf and other service specific configuration files which are stored in \/etc\/logrotate.d\/<br \/>\nSo main sample configuration is:<\/p>\n<pre># see \"man logrotate\" for details\n# rotate log files weekly specified in \/etc\/logrotate.d\/\nweekly\n# keep 4 weeks of all log files\nrotate 4\n# create new (empty) log files after rotating old ones\ncreate\n# use date as a suffix of the rotated file\ndateext\n# uncomment this if you want your log files compressed by gzip\ncompress\n# RPM packages drop log rotation information into this directory\n#there are all other configurations of services and their logs to rotate\ninclude \/etc\/logrotate.d<\/pre>\n<h4>Some samples and real log files configurations<\/h4>\n<p>So, we can add a new logs file into \/var\/log\/\u00a0 by this way:<\/p>\n<pre>echo \"this is a sample log file\" &gt; \/var\/log\/vasil.log\n#this create a log file vasil1.log of size 5MB\ndd if=\/dev\/zero of=\/var\/log\/vasil1.log bs=1M count=5<\/pre>\n<p>Next, we create a new configuration files which are stored in destination explained above:<\/p>\n<pre>vim \/etc\/logrotate.d\/vasil\n###\n\/var\/log\/vasil.log {\n missingok\n notifempty\n compress\n minsize 1M\n daily\n create 0600 root root\n}\n<\/pre>\n<pre>vim \/etc\/logrotate.d\/vasil1\n###\n\/var\/log\/vasil1.log {\n missingok\n notifempty\n compress\n minsize 1M\n daily\n create 0600 root root\n}<\/pre>\n<p>And som explanation of variables:<\/p>\n<ul>\n<li><strong>missingok<\/strong> &#8211; do not output error if logfile is missing<\/li>\n<li><strong>notifempty<\/strong> &#8211; do not rotate log file if it is empty<\/li>\n<li><strong>compress<\/strong> &#8211; Old versions of log files are compressed with gzip by default<\/li>\n<li><strong>minsize<\/strong> &#8211; Log file is rotated only if it is bigger than 1M<\/li>\n<li><strong>daily<\/strong> &#8211; ensures daily rotation<\/li>\n<li><strong>create<\/strong> &#8211; creates a new log file with permissions 600 where owner and group is root user<\/li>\n<\/ul>\n<p>If you want more options and their explanation, look into manual:<\/p>\n<pre>man logrotare<\/pre>\n<p>Look at list of \/var\/log for our log files. We can see, that we have one log vasil.log with size 26b and vasil1.log with size 5MB.<\/p>\n<pre>ls -lah \/var\/log\/va*\n-rw-r--r--. 1 root root 5.0M Mar\u00a0 3 13:21 \/var\/log\/vasil1.log\n-rw-r--r--. 1 root root\u00a0\u00a0 26 Mar\u00a0 3 13:21 \/var\/log\/vasil.log<\/pre>\n<p>Now, we can debug our configuration via this command:<\/p>\n<pre>logrotate -d \/etc\/logrotate.d\/vasil1\nor\nlogrotate -d \/etc\/logrotate.d\/vasil<\/pre>\n<p>So, if we want to run logrotate manualy and see, what is happend, run the following command. But be aware because it rotate all your logs, defined in \/etc\/logrotate.d\/<\/p>\n<pre>logrotate -f \/etc\/logrotate.conf<\/pre>\n<p>And we can see both log files compressed and two new empty log files created:<\/p>\n<pre>\u00a0ls -lah \/var\/log\/va*\n-rw-------. 1 root root\u00a0\u00a0\u00a0 0 Mar\u00a0 3 13:23 \/var\/log\/vasil1.log\n-rw-r--r--. 1 root root 5.0K Mar\u00a0 3 13:21 \/var\/log\/vasil1.log-20170303.gz\n-rw-------. 1 root root\u00a0\u00a0\u00a0 0 Mar\u00a0 3 13:23 \/var\/log\/vasil.log\n-rw-r--r--. 1 root root\u00a0\u00a0 44 Mar\u00a0 3 13:21 \/var\/log\/vasil.log-20170303.gz<\/pre>\n<p>We can look into our compressed log file by this command:<\/p>\n<pre>zcat \/var\/log\/vasil.log-20170303.gz\nthis is a sample log file<\/pre>\n<p>Or we can use gunzip to uncompress them by command gzip.<br \/>\nWhen we use logrotate, sometimes we need restart an application or service. Logrotate can do that by script called &#8220;postrotate&#8221;. This script can be used in configuration file like httpd. When log are rotated,\u00a0 script reload service to use new empty log file.<\/p>\n<pre>cat \/etc\/logrotate.d\/httpd\n\/var\/log\/httpd\/*log {\n\u00a0\u00a0\u00a0 missingok\n\u00a0\u00a0\u00a0 notifempty\n\u00a0\u00a0\u00a0 sharedscripts\n\u00a0\u00a0\u00a0 delaycompress\n\u00a0\u00a0\u00a0 postrotate\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/bin\/systemctl reload httpd.service &gt; \/dev\/null 2&gt;\/dev\/null || true\n\u00a0\u00a0\u00a0 endscript\n}<\/pre>\n<p>So I hope, that this how to helps somebody \ud83d\ude42 Have a fun.<\/p>\n ","protected":false},"excerpt":{"rendered":"<p>Yesterday, I met with problem of low capacity \/var\/log\/ partition. Some logs were too big and logrotate is the perfect tool to handle this problem. It is a software designed for reduce amount of space for every log file we have. And it can be done with some ways. Logrotate Description: logrotate\u00a0 is\u00a0 designed\u00a0 to\u00a0 &hellip; <a href=\"https:\/\/www.gonscak.sk\/?p=182\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Setting up logrotate on Centos 7<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[3,38],"class_list":["post-182","post","type-post","status-publish","format-standard","hentry","category-centos","tag-centos","tag-logrotate"],"_links":{"self":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182"}],"version-history":[{"count":1,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=\/wp\/v2\/posts\/182\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gonscak.sk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}