Sub topic 6
How logrotate runs and remembers state
This chapter assumes you can use the Linux command line, edit files with sudo, and read basic systemd unit status. You should also know that a logrotate configuration describes what to do with selected log files, while a scheduler starts logrotate at a regular time.
The scheduler starts logrotate
Logrotate does not normally run continuously. A scheduler starts it, and logrotate then reads its main configuration.
On many Linux systems, the traditional arrangement is:
/etc/cron.daily/logrotatestarts logrotate once per day./etc/logrotate.confcontains global settings./etc/logrotate.d/contains package or application-specific configurations./var/lib/logrotate/statusrecords when each log was last rotated.
Newer distributions may use a systemd timer instead. Check both possible mechanisms:
systemctl status logrotate.timer
ls -l /etc/cron.daily/logrotateOnly one mechanism should normally perform the daily run. Running both can cause confusing extra checks, although logrotate's state file usually prevents an immediate second rotation.
How the state file works
Logrotate checks each configured file against its schedule. For a daily rule, it records the date of the last rotation. If the required interval has not elapsed, it skips that file.
A simplified status file may look like this:
logrotate state -- version 2
"/var/log/myapp/app.log" 2026-11-28
"/var/log/nginx/access.log" 2026-11-27The scheduler's time and the file's last-rotation time are separate facts. The daily job can run at 00:05, but a file is rotated only when its own rule says it is due.
| Setting | Meaning |
|---|---|
| `daily` | Rotate when at least one day has elapsed |
| `weekly` | Rotate when at least one week has elapsed |
| `monthly` | Rotate on a monthly schedule |
| `size 100M` | Rotate when the file reaches the size threshold |
| `-f` | Force rotation even when it is not due |
| `-d` | Debug the decision without changing files |
The size directive has important interactions with time-based directives. In a rule containing both, logrotate can rotate when the size condition is met rather than simply waiting for the time interval. Test the exact rule on your distribution before relying on that behavior.
Inspecting and changing state safely
Use debug mode to see what logrotate would do. Debug mode does not modify logs, rename files, or update the state file.
sudo logrotate -d /etc/logrotate.confThe -f option forces rotation and does change files. Use it only when testing in a controlled way:
sudo logrotate -f /etc/logrotate.confYou can store state somewhere else with -s. This is useful for a test run, because it avoids changing the production state file:
sudo logrotate -d -s /tmp/logrotate-test.status /etc/logrotate.confDo not delete or edit /var/lib/logrotate/status casually. Removing it makes logrotate lose its history, so many rules may become eligible for rotation at the next run.
Trace the complete path
Exercise
Which command lets you inspect a scheduled logrotate run without changing log files or the state file?
Show answer
Use sudo logrotate -d /etc/logrotate.conf. The -d option enables debug mode.