Official website for Linux User & Developer
FOLLOW US ON:
Jan
26

‘Logrotate’ your Linux Log Files

by Swayam Prakasha

In all the above examples, we have used various directives such as rotate, compress, size, daily, weekly and monthly in logrotate configuration files. There are three commands that specify how often rotation should take place and they are daily, weekly and monthly. When we use compress, the old log files are compressed with gzip by default. Another directive ‘ifempty’ instructs the logrotate to rotate the file even if it is empty. By typing ‘man logrotate’ at the UNIX/Linux command prompt, one can get detailed information about the various directives that can be used in the configuration files.
It is very natural to see ‘rotate n’ in logrotate configuration file (where n is any positive integer). This command is slightly inaccurate – ‘n’ weeks’ worth of log files will be kept if we do the rotation weekly; if rotation is done on a daily basis, then we are keeping four days’ worth of log files.

We usually come across various other directives in the configuration file.
a. include /etc/logrotate.d instructs the log rotation program to read each file in the specified directory.
b. create – After moving the mail log file, create an empty, new log file.
c. dateext – Archive the old log files
by adding a date extension (in the YYYYMMDD format).
d. notifempty – Do not rotate the log file if the current log file is empty.
e. create 644 root root – After the rotation, create a new log file owned by user root and group root. Provide read–write permission to its owner and read permission to all
other users.
f. size=+2048 – Rotate the log file if it becomes larger than 2MB.
g. maxage 365 – Remove the rotated log files older than 365 days.
h. delaycompress – Postpone the compression of the previous log file to the
next cycle.
i. olddir dir- log files are moved into a directory – dir – for rotation. When we use this directive, all old versions of the log file are available in the specified directory.

The final step is to create a cron job that will run logrotate about once a day. We can do this by running crontab –e at the command prompt. If we want to run logrotate at 6:30, then the cron job looks like…

30 6 * * *  /usr/sbin/logrotate -s /home/swayam/cfg/logrotate.status /home/swayam/cfg/logrotate.conf

The first part of this cron job – 30 6 * * * – tells cron to run the job at 6:30 every morning. The second part – /usr/sbin/logrotate –s /home/swayam/cfg/logrotate.status /home/swayam/cfg/logrotate.conf – tells cron what command to run. Please note that logrotate is in /usr/sbin on our machine, but may be different on your machine. The –s option to logrotate tells it to keep the status information in /home/swayam/cfg/logrotate.status. This is very much needed because otherwise logrotate will keep this status information in a file we don’t have permission to access. Since we can’t access the file, it will then die of a permission error. The last argument – /home/swayam/cfg/logrotate.conf – tells logrotate which configuration file to use.
Once the cron job is set up, everything should be functioning as expected. It is always better to add the –f option – /usr/sbin/logrotate –f –s /home/swayam/cfg/logrotate.status /home/swayam/cfg/logrotate.conf. With this –f option, we can see instantly that things are working.

Sometimes you may be having problems with logrotate not doing the expected task. In that case, it is advisable to run it manually with the ‘-d’ option. Note that with –d, logrotate will not rotate the log files. Now you know how logrotate works, go ahead and use it in your daily life so that you can avail the full benefits offered by it.

Pages: 1 2 3

  • Tell a Friend
  • 4 Comments »

    • Ajeet said:

      Moderators !!!
      its Logrotate not Logrogate
      :)

      Anyway, it was informative article.

    • RussellBarnes (author) said:

      Well spotted! Glad you liked the article.

    • www.mavendo.de said:

      ‘Logrotate’ your Linux Log Files…

      A log file grows without limits unless some action is taken. Growing log files pose many problems since larger files are very difficult to manipulate and file systems can run out of space……

    • Nedko Iliev said:

      Considered to this nice article, I would like to ask is there some option on rotate newest archive to be with greatest number, dateext is not an option, because my rotates are hourly.

    What's your opinion?

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.