All articles

Test a logrotate configuration before rotation

5 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Test a logrotate configuration before rotation

A logrotate rule can parse successfully and still rotate at the wrong time, keep the wrong number of archives, or break an application that keeps its log file open. Start with debug output and a copied rule. Save forced rotation for the end of the investigation.

Build a disposable test

lab=$(mktemp -d)
log="$lab/app.log"
config="$lab/logrotate.conf"

printf 'first line\n' > "$log"
cat > "$config" <<EOF
$log {
    size 1
    rotate 3
    missingok
    notifempty
}
EOF

logrotate --debug --state /dev/null "$config"

Debug mode prints what logrotate would do without rotating logs or updating the state file. Passing /dev/null as the state path keeps this lab separate from the host’s normal rotation history.1

Remove the lab with trash "$lab" if trash is installed.

Understand why nothing rotated

Time-based rules depend on recorded rotation history and on how often logrotate runs. Size-based rules depend on the current file size. A first debug run can therefore say a rule is valid while correctly deciding that no rotation is due.1

Inspect the live configuration and state without changing them:

sudo logrotate --debug /etc/logrotate.conf
sudo sed -n '1,80p' /var/lib/logrotate/status

Debug mode only reads, but it must run with the same privileges as the scheduled service, and with the same --state path when that service uses a non-default one. Unprivileged, logrotate cannot open the state file, assumes an empty history, and fails the user switches requested by su directives, so its decisions no longer describe the real host.1

The state location can differ by distribution. Use the path printed by your scheduled service or package configuration rather than assuming it.

Check how rotation is scheduled

On systemd-based distributions:

systemctl status logrotate.timer logrotate.service
systemctl list-timers logrotate.timer
systemctl cat logrotate.timer logrotate.service
journalctl -u logrotate.service --no-pager

Timer activation may include accuracy windows, persistence behavior, and calendar rules. The effective schedule is a property of both the timer and when the machine is running.2

Review file ownership and reopen behavior

Rotation is only half the job. Some applications reopen logs after a signal or a postrotate command. Others require copytruncate, which has a small window where data can be lost between copying and truncating. Prefer native reopen support when the application offers it.1

Check the expected owner, group, mode, SELinux context, and application behavior before applying a new rule.

[!CAUTION] logrotate --force changes files and may run scripts from the configuration. Do not force the system-wide configuration as a syntax test. Use --debug first, and force only a controlled copy or a maintenance-approved production rule.1

Sources and further reading
  1. logrotate(8)
  2. systemd.timer