Sub topic 6
How logrotate runs and remembers state
This chapter assumes you can write a stanza like the ones in the previous chapter. It covers what happens after you save the file: who runs logrotate, how it decides a log is due, and where it keeps its memory between runs. Most "logrotate did not rotate" problems come down to one of these three things.
logrotate is not a daemon
logrotate is a short-lived program. It reads its configuration, checks each log file, rotates the ones that are due, and exits. It has no scheduler of its own. Something else must start it, and that something sets the finest rotation interval you can have.
Two launchers are common:
- A systemd timer,
logrotate.timer, which startslogrotate.service. Current Debian, Ubuntu, and RHEL-family releases use this. - A script at
/etc/cron.daily/logrotate, run by cron or anacron. Older systems and some minimal installs still use this.
Check which one your host uses:
systemctl list-timers logrotate.timer
systemctl cat logrotate.timer logrotate.service
ls -l /etc/cron.daily/logrotateA typical timer looks like this:
[Unit]
Description=Daily rotation of log files
[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.targetPersistent=true matters on servers that are sometimes off. If the machine was down at the scheduled time, systemd runs the job at the next boot instead of skipping it. Cron has no such feature, which is why /etc/cron.daily relies on anacron on machines that are not always on.
The service then runs logrotate /etc/logrotate.conf. That file sets global defaults and ends with an include /etc/logrotate.d line. Every file in that directory is read as more stanzas, so /etc/logrotate.d/remote from the central server is loaded on every run.
Rotation frequency is a check, not a timer
The words daily, weekly, and monthly do not tell logrotate when to run. They tell it what to decide each time it is started. For every log file, logrotate compares the current time with the last rotation time that it recorded for that file. That record lives in the state file.
dailyrotates when the last rotation was on a different calendar day.weeklyrotates when the weekday has wrapped around since the last rotation, or more than a week has passed.monthlyrotates on the first run in a new calendar month.hourlyrotates when the hour has changed. This only works if logrotate itself is started at least hourly.
Two consequences follow:
- If logrotate is started once a day at 03:00, a
dailylog rotates at about 03:00. A stanza sayinghourlywill still rotate at most once a day, because nothing starts logrotate more often. - Running logrotate twice on the same day does not rotate a
dailylog twice. The second run sees today's date in the state file and skips it.
Size-based directives work the same way but add a size test:
| Directive | Rotates when |
|---|---|
| `size 100M` | The file is larger than 100 MB, regardless of time. Time-based directives are ignored. |
| `minsize 100M` | The interval is due and the file is at least 100 MB. |
| `maxsize 100M` | The interval is due, or the file exceeds 100 MB. |
The size tests only run when logrotate runs. A log can grow far past maxsize between daily runs.
To get hourly checks, override the timer with a drop-in:
# /etc/systemd/system/logrotate.timer.d/hourly.conf
[Timer]
OnCalendar=
OnCalendar=hourlyThe empty OnCalendar= line clears the inherited value. Without it, systemd keeps both schedules. Apply the change with sudo systemctl daemon-reload, then run systemctl list-timers logrotate.timer to confirm the new next-run time.
The state file
logrotate remembers the last rotation time of each log in a plain text file. The default path depends on the distribution:
- Debian and Ubuntu:
/var/lib/logrotate/status - RHEL, Fedora, and derivatives:
/var/lib/logrotate/logrotate.status
Look at it:
sudo head -n 5 /var/lib/logrotate/statuslogrotate state -- version 2
"/var/log/syslog" 2026-11-28-3:0:0
"/var/log/remote/web01/nginx.log" 2026-11-28-3:0:0
"/var/log/remote/db01/postgres.log" 2026-11-27-3:0:0Each line holds a log file path and the date and time of its last rotation. The path is the key. The consequences are worth knowing:
- A new log file has no entry. logrotate records today's date on its first run and does not rotate it until the interval passes. A freshly added file will not rotate on the first night.
- With a wildcard such as
/var/log/remote/*/*.log, each file that matches gets its own entry. When a new host starts sending logs, its files appear in the state file on the next run. - If you rename a log, logrotate sees a new path with no history.
- Entries for files that no longer exist stay in the file. Old entries are harmless, though they accumulate.
- The state file must be writable by the user running logrotate. If it is not, logrotate fails with a state file error and the rotation is not recorded.
Using state to test and to recover
Because rotation depends on recorded dates, you can control tests by changing what logrotate remembers. Use a separate state file with -s so the real one stays untouched:
# Dry run with the real state: shows decisions, changes nothing
sudo logrotate -d /etc/logrotate.d/remote
# Force one rotation using a scratch state file
sudo logrotate -f -s /tmp/lr-test.status /etc/logrotate.d/remote
cat /tmp/lr-test.statusKnow what each option does to state:
| Option | Rotates files | Updates state file |
|---|---|---|
| `-d` (debug) | No | No |
| `-v` (verbose) | Yes, if due | Yes |
| `-f` (force) | Yes, even if not due | Yes |
| `-s FILE` | As configured | Writes to `FILE` instead |
Using -f on the real state file is not free. It records today as the last rotation, so the next scheduled run skips that log. After a forced rotation of a daily log, the next regular rotation will be tomorrow's run.
To make a log look overdue, edit its date in the state file to an earlier day and run logrotate -d to see the decision. Do this on a scratch copy of the state file.
Common failures related to scheduling and state
- A log never rotates. Check that the timer is active, that the file has a state entry, and that the interval has passed since that entry's date.
logrotate -dprints "log does not need rotating" with the reason. - Rotation happens, but the first night does nothing. The file was new. See the state file section.
duplicate log entryerror. The same path matches two stanzas, for example one inlogrotate.confand one inlogrotate.d/remote. logrotate refuses to guess, so fix the overlap.- Hourly rotation does not happen hourly. The timer or cron job only starts logrotate daily. Add the timer drop-in above.
- Rotation is late after downtime. Without
Persistent=true, or without anacron, a missed run is not made up. - Forced rotation, then nothing the next day.
-fupdated the state. This is expected.
Exercise
A central server has the stanza daily for /var/log/remote/*/*.log. Yesterday you ran sudo logrotate -f /etc/logrotate.d/remote at 16:00 to free space. The timer runs at 03:00. Will the timer's 03:00 run rotate these logs tonight, and which part of logrotate's behaviour explains your answer?
Show answer
Yes, it will. The forced run recorded the date of yesterday in the state file. At 03:00 the date has changed, so daily sees a different calendar day and the logs are due. If you had run the forced rotation today, shortly before the check, the 03:00 run would have found the same date in the state file and skipped them. logrotate decides by comparing the current date with the date saved in the state file, not by counting 24 hours.