Log rotation in Linux systems

Using log rotation helps you manage your log files, so they don't keep growing indefinitely.


This how-to is about rotating log files. Log rotation is useful for many reasons, but most importantly, it organizes logs by date instead of letting a single file grow indefinitely. Additionally, the rotated files are compressed and removed after a predetermined period.

Here you’ll find an example of how the logs can be rotated for a fictional application, myapp.

The following should be placed in /etc/logrotate.d/myapp.

/etc/logrotate.d/myapp
/home/myuser/log/myapp/*.log {
       weekly
       missingok
       notifempty
       rotate 20
       compress
       delaycompress
       create 640 myuser mygroup
}

In the example, we have an application named myapp that runs under the user myuser and group mygroup. The application writes its logs to the directory /home/myuser/log/myapp, producing two files there: info.log and error.log.

From top to bottom, we can see that:

  • All log files are rotated weekly.

  • Empty files are not rotated.

  • If a file is missing, no error message is generated.

  • Twenty weeks of history are retained.

  • The rotated files are compressed.

  • The most recently rotated file is left uncompressed.

  • After rotation, a new empty file is created with permissions 640 and ownership myuser:mygroup.

Last updated

Was this helpful?