Sub topic 5
Writing logrotate configurations
This chapter covers the directives you combine to build a working logrotate stanza, how each one behaves, and the mistakes that cause lost log lines or full disks. It assumes you are comfortable editing files as root, know basic systemd usage (systemctl, unit status), and have seen the /var/log directory. Scheduling and the state file are covered in the next chapter.
The shape of a stanza
A logrotate configuration is a list of stanzas. Each stanza has one or more file patterns, then directives in braces. Directives outside any braces are global defaults for the rest of the file.
# /etc/logrotate.d/myapp
/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 main file /etc/logrotate.conf sets global defaults and uses include /etc/logrotate.d to read per-application files. Put your own stanzas in /etc/logrotate.d/, one file per application. A directive inside a stanza overrides the global default.
The rotation cycle
When logrotate decides a log is due, it renames the files, creates a fresh one, and removes the oldest. With rotate 3, the files shift like this:
The key point is that logrotate only renames files. A running process holds its log file open by file descriptor, so after the rename it keeps writing to app.log.1. The service must be told to reopen its log path. That is the job of postrotate (or copytruncate, covered below).
Choosing when to rotate
Pick one of two kinds of trigger.
- Time-based:
hourly,daily,weekly,monthly,yearly. These only take effect if logrotate runs often enough. Anhourlystanza does nothing if logrotate only runs once a day. - Size-based:
size 100Mrotates when the file exceeds the size, regardless of time.maxsize 100Mrotates when the file exceeds the size or the time interval passes, whichever comes first.minsize 1Mrotates on schedule only if the file is at least that big.
Use maxsize together with daily for logs that normally stay small but sometimes burst. Plain size ignores the time directives, so a quiet log can sit unrotated for months.
Choosing how many to keep
rotate Nkeeps N old generations.rotate 0deletes old logs instead of keeping them.maxage Nremoves rotated files older than N days, even if fewer than N generations exist.dateextnames rotated files with a date (app.log-20261128) instead of a number. This avoids renaming every old file on each rotation and makes files easier to identify. Adddateformat -%Y%m%d-%sif you rotate more than once per day, because two rotations on the same date would otherwise collide.start 0orextension .logadjust numbering and keep a suffix such as.logat the end of the name, which helps tools that match on file extension.
Compression
compress gzips rotated files. Three companions matter:
delaycompressleaves the newest rotated file (app.log.1) uncompressed until the next cycle. Use it when a process may still write to the old file after the rotation, because compressing a file that is still open loses or corrupts data.compresscmdandcompressextselect another tool such asxz(compresscmd /usr/bin/xz,compressext .xz).nocompressturns compression off for a stanza when a global default turned it on.
Handling missing and empty files
missingokskips the stanza silently if the log does not exist. Without it, logrotate prints an error and returns a non-zero status.notifemptyskips rotation when the file has zero bytes. Without it, empty files are rotated and old data is pushed out of your retention window by empty generations.ifemptyis the opposite and is the default.
Creating the new file
create MODE OWNER GROUP makes a new empty log after the rename, with the given permissions. In the example, create 0640 myapp adm produces a file the myapp user can write and the adm group can read. If you omit the arguments, logrotate copies the mode and owner of the old file.
If the service creates its own log file on startup, use nocreate. If the stanza's directory is not writable by the service user, use su USER GROUP so logrotate runs as the owner. This also avoids the "insecure permissions" error that logrotate raises when a log directory is writable by a non-root group.
Telling the service to reopen its log
There are three approaches, in order of preference.
- Signal the service in
postrotate. Many daemons reopen logs onSIGHUP. On systemd systems,systemctl kill -s HUP unit.servicesends the signal to the unit's main process without needing a PID file. Check the service's documentation, because for some daemonsSIGHUPmeans a full config reload, and others useSIGUSR1or a dedicated command. - Use the service's own reopen command, for example
nginx -s reopen. copytruncate. Logrotate copies the file, then truncates the original in place. The service never needs to reopen anything. The cost is a window between the copy and the truncate in which lines are lost, and the copy temporarily doubles disk use. Use it only for programs that cannot reopen their logs.
Script directives
| Directive | When it runs |
|---|---|
| `prerotate` | Before rotation, only if the log will be rotated |
| `postrotate` | After rotation, before compression |
| `firstaction` | Once, before any file in the stanza is rotated |
| `lastaction` | Once, after all files are rotated |
Every script block ends with endscript. By default, if the stanza matches several files (a wildcard), prerotate and postrotate run once per file. Add sharedscripts to run them once for the whole stanza, and only if at least one file was rotated. For a *.log pattern, this is almost always what you want, because otherwise a daemon receives a HUP for every file.
If a script exits with a non-zero status, logrotate reports an error and, for prerotate, skips rotation of that file.
Putting it together on a central server
The central receiver from this tutorial writes one file per host and program, and it needs a stanza that matches the nested path:
# /etc/logrotate.d/remote on the central server
/var/log/remote/*/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
sharedscripts
postrotate
/usr/bin/systemctl kill -s HUP rsyslog.service
endscript
}sharedscripts matters here. A receiver with fifty hosts and several programs each would otherwise send hundreds of HUPs to rsyslog in a single run. Omitting create is deliberate: rsyslog creates new files itself, with the mode set in its own configuration.
Checking a stanza before relying on it
Use debug mode first. It reads the configuration, prints what would happen, and changes nothing.
sudo logrotate -d /etc/logrotate.d/myappIf the output says considering log ... log does not need rotating, the trigger has not fired yet. To exercise the real behavior, force one rotation:
sudo logrotate -f /etc/logrotate.d/myapp
ls -l /var/log/myapp/Forcing a rotation updates the state file, so a following normal run will treat the log as freshly rotated.
Common errors
- "error: skipping ... because parent directory has insecure permissions." The directory is writable by a group or others. Fix the permissions, or add
su USER GROUP. - The service keeps writing to the old file. The
postrotatesignal was not sent or not understood. Check the unit name, and confirm that the daemon reopens logs on that signal. - Rotated files are never removed. Your pattern may not match the rotated names, or
rotateis larger than you expect. Checkrotate,maxage, and whether another stanza also matches the same file. Logrotate reports a "duplicate log entry" error when two stanzas match the same path. - Compressed file is truncated or incomplete. A process still wrote to the old file while it was compressed. Add
delaycompress. - Syntax errors silently disable a file. Run
logrotate -dafter every edit.
Exercise
A central server stores logs at /var/log/remote/web1/nginx.log and similar paths for 40 hosts. Your stanza uses a wildcard and a postrotate script that sends HUP to rsyslog, but you did not add sharedscripts. What happens, and how do you fix it?
Show answer
The postrotate script runs once per rotated file, so rsyslog receives dozens of HUP signals in one logrotate run. Add sharedscripts to the stanza so the script runs once, after all matching files are rotated, and only if at least one was rotated.
Exercise
A daemon cannot reopen its log file on any signal. Which directive lets you rotate its log, and what is the cost?
Show answer
copytruncate. Logrotate copies the log and then truncates the original in place, so the daemon keeps its open file descriptor. Lines written between the copy and the truncate are lost, and the copy temporarily needs extra disk space.