Logrotate

logrotate is a utility that is generally always installed on servers and in the true Unix tradition, it has one simple job and it does it well.

What does logrotate Do?

What it does is allow you to freely log to a specific file location, but for that log file to be "rotated" which stops it from just growing in size indefinitely.

The rotation logic is configurable, but in general there will be one active log file that is still having things appenedd to it, and then an archive of other log files, often compressed. Once a log file archive gets beyond a certain age or number then it will be removed. This means that in general the amount of disk space being consumed by a particular log should stay roughly the same.

Generally your server or system will already have a bunch of things configured for logrotate to handle, however you can also add your own.

Configuration

The configuration files for logrotate jobs generally reside in /etc/logrotate.d

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#/etc/logrotate.d/nginx
/var/log/nginx/*log {
    su nginx nginx
    daily
    rotate 5
    size 1000k
    delaycompress
    missingok
    notifempty
    compress
    sharedscripts
    create 640 root root
    postrotate
      [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

The file format is quite simple. The first line is a glob, generally ending in *log. Don't ever have one that ends with * otherwise you will try to rotate already rotated things!

Then we have a block wrapped in { } and inside here we have the configuration parameters.

To get the full list of configuration directives you can use, you should look at man logrotate

Further Reading

Creating your Own

First, have a read through the man page and reference some other config files.

Next, your starting file contents should look like this:

1
2
3
/abs/path/to/folder/*.log{

}

This assumes the files you want to rotate have a log extension

The directives are up to you, but a fairly standard set might look like:

1
2
3
4
5
6
7
8
9
/abs/path/to/folder/*.log{
  daily
  rotate 7
  missingok
  compress
  copytruncate
  notifempty
  create 660 username username
}

Testing

You have 2 main ways of testing:

Dry Run

If you want to test your file without doing anything then use the -d flag to logrotate:

1
logrotate -d /etc/logrotate.d/mylogrotatefile

Run Verbose

If you want to actually run it but get debugging output, try

1
logrotate -d /etc/logrotate.d/mylogrotatefile