Sub topic 5
Writing logrotate Configurations
Assume you can edit files with sudo, understand /var/log, and know how to restart or signal a systemd service. This chapter focuses on rotating ordinary text log files. logrotate does not manage the binary journal maintained by systemd-journald.
What a logrotate rule does
A logrotate rule describes when to rename a log, how many old copies to keep, whether to compress them, and what the application must do afterward. Rotation prevents one active file from growing until it fills the disk.
A typical rotation follows this sequence:
The active file usually keeps its original name, such as app.log. Older files receive suffixes such as app.log.1 and app.log.2.gz.
Important directives
| Directive | Purpose |
|---|---|
| `daily` | Consider the file for rotation every day |
| `rotate 14` | Keep 14 old generations |
| `compress` | Compress rotated files with gzip |
| `delaycompress` | Leave the newest rotated file uncompressed until the next cycle |
| `missingok` | Do not report an error if the file does not exist |
| `notifempty` | Do not rotate an empty file |
| `create 0640 user group` | Create the replacement file with a mode, owner, and group |
| `sharedscripts` | Run a `postrotate` script once for the whole matching block |
daily is a schedule check, not a guarantee that every run rotates the file. Logrotate also records its previous actions in a state file and normally avoids rotating the same file twice in one period.
A complete application rule
Create /etc/logrotate.d/myapp with a rule matching the application's log files:
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 myapp adm
sharedscripts
postrotate
/usr/bin/systemctl kill -s HUP myapp.service
endscript
}The postrotate command sends the HUP signal after rotation. Many daemons respond by closing their old file descriptor and opening the path again. Without this step, an application may continue writing to the renamed file, making the new file appear unused.
The create directive must match the application's expected permissions. The myapp user must be able to write the new file. On systems using access controls such as SELinux, preserve the expected security context as well.
Choosing a safer rotation method
Renaming and recreating a file is usually preferable because it avoids copying a large file while the application writes to it. It requires the application to reopen its log.
copytruncate is a fallback for applications that cannot reopen logs:
/var/log/legacy/*.log {
weekly
rotate 8
compress
missingok
notifempty
copytruncate
}This copies the file and then truncates the original. Writes can be lost or duplicated during the copy, so use it only when signaling or restarting the application is impossible.
Exercise
Why does a rule that renames a log normally need a postrotate action?
Show answer
The application may keep its old file descriptor open. The postrotate action tells it to close that descriptor and reopen the log path.