{"id":699,"date":"2016-07-08T17:40:48","date_gmt":"2016-07-08T12:10:48","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=699"},"modified":"2016-07-08T17:41:21","modified_gmt":"2016-07-08T12:11:21","slug":"699-2","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/699-2\/","title":{"rendered":"Enabling log rotation on Linux Hosts"},"content":{"rendered":"<p>Log rotation is a way of managing old log files to reduce disk space usage on the server.<\/p>\n<p>By default logrotate is invoked once a day using a cron scheduler from location \/etc\/cron.daily\/<\/p>\n<p><code>grep logrotate.conf \/etc\/cron.daily\/logrotate<\/code><br \/>\n<code> \/usr\/sbin\/logrotate \/etc\/logrotate.conf &gt;\/dev\/null 2&gt;&amp;1<\/code><\/p>\n<p>Logrotation is manged by two different configuration files <b>\/etc\/logrotate.conf<\/b> [generic file] and a service specific configuration file placed under <b>\/etc\/logrotate.d\/ <\/b>folder.<\/p>\n<p>In our recent post, we have added how to <a href=\"http:\/\/wp.me\/p7F4uM-aV\" target=\"_blank\" rel=\"noopener\">enable cmdlog<\/a> on linux host. Let&#8217;s configure logrotation for the same. For example, we have to add below contents in \/etc\/logrotate.d\/cmdlog [name can be anything ] file to enable logrotation for \/var\/log\/cmdlog.log.<\/p>\n<p><code>\/var\/log\/cmdlog.log<\/code><br \/>\n<code> {<\/code><br \/>\n<code> missingok<\/code><br \/>\n<code> notifempty<\/code><br \/>\n<code> size 200M<\/code><br \/>\n<code> rotate 5<\/code><br \/>\n<code> compress<\/code><br \/>\n<code> create 0600 root root<\/code><br \/>\n<code> sharedscripts<\/code><br \/>\n<code> postrotate<\/code><br \/>\n<code> \/bin\/kill -HUP `cat \/var\/run\/syslogd.pid 2&gt; \/dev\/null` 2&gt; \/dev\/null || true<\/code><br \/>\n<code> endscript<\/code><br \/>\n<code> }<\/code><\/p>\n<p>Where,<\/p>\n<p><b>\/var\/log\/cmdlog.log :<\/b> Log file for which rotation is needed; We can specify multiple log files[one per line] or else use regex to cover multiple log files. Example : \/var\/log\/*.log can be used.<\/p>\n<p><b>missingok :<\/b> Do not output error if logfile is missing.<\/p>\n<p><b>notifempty :<\/b> Do not rotate log file if it is empty<\/p>\n<p><b>size :<\/b> Log file is rotated only if it grow bigger than the specified size.<\/p>\n<p><b>rotate :\u00a0<\/b> ensures that logrotate keeps a specified number of backup of log files.<\/p>\n<p><b>compress :<\/b> Old versions of log files are compressed with gzip by default.<\/p>\n<p><b>create :<\/b> Creates a new log file wit permissions 600 where owner and group is root user.<\/p>\n<p><b>daily, weekly,\u00a0 monthly,\u00a0 or\u00a0 yearly :<\/b> Backup rotation interval<\/p>\n<p><b>prerotate &amp; postrotate : <\/b>In some cases, we might have to restart a service or kill a process before or after rotating logs in such cases we can use these options.<\/p>\n<p>For more available options, try &#8220;<b>man logrotate&#8221;<\/b> command.<\/p>\n<p>Once above file is created, either we can wait for logrotation to happen when the cron runs or else we can issue below command to run logrotate forcefully.<\/p>\n<p><code>logrotate -f \/etc\/logrotate.conf<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Log rotation is a way of managing old log files to reduce disk space usage on the server. By default [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-699","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-bh","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=699"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/699\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}