How to enable log rotation on Linux

logrotate is a program which will automatically backup your old log files and gzip them. You can specifiy how often logrotate should backup your logfiles and how long it should keep them. The advantage of logrotation is that you can save disk space without the deletion of log files. The logrotation can be configured for automatic rotation, compression, removal, and mailing of log files.


The default configuration file is /etc/logrotate.conf


    # see "man logrotate" for details
    # rotate log files weekly
    weekly


    # keep 4 weeks worth of backlogs
    rotate 4


    # create new (empty) log files after rotating old ones
    create


    # uncomment this if you want your log files compressed
    #compress


    # RPM packages drop log rotation information into this directory
    include /etc/logrotate.d


    # no packages own wtmp -- we'll rotate them here
    /var/log/wtmp {
    monthly
    minsize 1M
    create 0664 root utmp
    rotate 1
    }


    # system-specific logs may be also be configured here.




Service or server specific configurations stored in /etc/logrotate.d directory, for example here is sample apache logrotate configuration file:
less /etc/logrotate.d/httpd


    /var/log/httpd/*log {
    monthly
    rotate 52
    compress
    missingok
    notifempty
    sharedscripts
    postrotate
    /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
    }


Now you need to set a cronjob for the logrotation to run. crontab -e


00 00 * * * /usr/sbin/logrotate -s /home/humanlinux/config/logrotate.status


Cron ensures that the command runs at midnight everyday. 
The command has three parts. 
/usr/sbin/logrotate is the path to logrotate. 
The -s /home/humanlinux/config/logrotate.status option specifies where logrotate keeps its status information. 
This file has to be writeable by the user running the cron.


run the command /usr/sbin/logrotate -f /etc/logrotate.conf to begin the log rotation

0 comments:

Post a Comment