Rotate logs using log rotate

The /var/log/messages file has grown to well over 7.1GB which isn’t very good especially when you want to read through it. In order to make this more manageable, you should probably rotate the file.

One tool for rotating logs is logrotate. The following is the configuration file in charge of /var/log/messages.

cat /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

In order to have logrotate rotate the logs weekly, ensure your configuration is similar to:

cat /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
    # Run the pre/post rotate commands only once for all the logs
    # that are being rotated.
    sharedscripts

    # rotate weekly
    weekly

    # keep 2 years of logs
    rotate 104

    # send HUP to syslogd
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

You may also want to do other things such as email notifications or compression, which is available as well. You will want to refer to RedHat’s Deployment Guide for more information.

You may also want to have logrotate run automatically via cronjobs.

crontab -l
@daily /usr/sbin/logrotate /etc/logrotate.conf > /dev/null 2>&1

Leave a Reply

Your email address will not be published.