You are here

Rotating Log Files in Linux

Applications usually generate a huge amount of log files. Some logging utilities available to programming languages already have the rotating (or rolling) functionality. If this is not the case and you are using some flavor of Linux or Unix to run your system, you might use the logrotate utility. Check if you have it available by typing:

[definenull@hm1446 prp]$ whereis logrotate
logrotate: /usr/sbin/logrotate /etc/logrotate.conf /etc/logrotate.d /usr/share/man/man8/logrotate.8.gz


You will need to provide a configuration file to the logrotate command. Something like this should be in the content of, let's say, "logrotate.cfg" file:

/home/definenull/rails_app/prp/log/*.log {
    weekly
    rotate 10
    missingok
    compress
}


You can have as many sections like this one in your configuration file. These options above mean:

  • /home/definenull/rails_app/prp/log/*.log: is the path to the log files you want to rotate
  • weekly: rotates every week, .i.e., log files are rotated if the current weekday is less then the weekday of the last rotation or if more then a week has passed since the last rotation.
  • rotate 10: logfiles are rotated 10 times before being deleted or emailed
  • missingok: if the log file is missing, no error will be printed
  • compress: will compress the rotated files

This is a basic simple configuration file and you can find more instructions on logrotate by simply typing man logrotate on your console.
I've created a basic shell script file to be able to run the logrotate command easily and here it is:

#!/bin/sh
/usr/sbin/logrotate -s /home/definenull/logrotate.status /home/definenull/logrotate.cfg


The -s option uses a state file other than the original. The documentation explains that this is useful if you want to run the command to many users at the same time, but in my case, it was useful because I did not have sudo and was denied to write in the original file "/var/lib/logrotate.status".
Now just add an entry to your cron tab with the frequency you want this job to run and you will have some control over the growth of your log files.