Logrotate – Linux Log Files Management Tool

By Kwok WhyeNo Comments

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression,removal, and mailing of log files. Each log file may be handled daily,weekly, monthly, or when it grows too large.

For detail command line option for logrotate, you can refer http://linuxcommand.org/man_pages/logrotate8.html.

Logrotate Installation
Normally, logrotate had been bundle together with Centos.
However, you can upgrade and install logrotate with a simple

yum install logrotate

Logrotate Configuration
Logrotate configuration file is stored in folder /etc/logrotate.d/ .
Inside this folder, it will a lot of configuration file to monitor different log. You can create a new file with the following format to monitor the desired log files. Refer the link above to understand the option in the configuration file.

/var/application_name/*.log {
      create 644 sigma wheel
      # olddir /log/adweb
      rotate 1000
      # size 1M

Daily cronjob issue with logrotate
logrotate will be triggered by daily cronjob in Linux server. You can find the logrotate in /etc/cron.daily/ folder. Due to daily saving, cron.daily is configured to run in 4.02 am everyday as following

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

In this case, you might see your log file will have extra 4 hour ( 12am – 4.02am ) log in previous log file.
To fix this problem, you can either change the /etc/cron.daily to run on 12.00am, but this might impact all the cronjob in /etc/cron.daily.

Another way to fix this problem is, create another folder name /etc/cron.midnight and copy logrotate from /etc/cron.daily to /etc/cron.midnight and configure in /etc/crontab as following.

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
0  0 * * * root run-parts /etc/cron.midnight
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

In this case, you will have accurate full day log files rotated on 12 midnight.


Leave your Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Blue Taste Theme created by Jabox