Sub topic 11
Missed runs: Persistent timers and anacron
This chapter assumes you can write a basic cron entry and a systemd service and timer pair. It also assumes you can read journalctl output and know what a job's exit status means. The topic is what happens when the scheduled moment passes and nothing runs.
What a missed run is
A scheduler fires a job when the clock reaches a time. If the machine is powered off, suspended, or the scheduler itself is not running at that moment, the moment passes. Whether the job runs later depends on the scheduler and its configuration.
Common causes of a missed run:
- The machine was shut down overnight, which is typical for laptops, desktops, and cloud instances that are stopped to save money.
- The machine rebooted for a kernel update and was down during the scheduled minute.
- The machine was suspended or hibernated.
- The cron daemon or the systemd manager was stopped or crashed.
- The system clock jumped forward past the scheduled time, for example after a bad time sync.
Classic cron does not catch up. If the daemon is not running at 02:30, the 02:30 job is skipped, and cron will not run it when it starts again at 02:45. Nothing is logged about the skipped run, because from cron's point of view nothing happened.
This is a "silent failure" of the kind described at the start of the tutorial. The job is correct and the schedule is correct, but the run never occurs.
Deciding whether you need catch-up
Catch-up is not always what you want. Decide per job.
| Job type | Catch up after a missed run? | Reason |
|---|---|---|
| Nightly backup | Yes | A missing backup leaves a gap in your recovery points. |
| Log rotation | Yes | Logs keep growing until rotation runs. |
| Package index refresh | Yes | Running late is as good as running on time. |
| Report emailed at 08:00 | Usually no | A report that arrives at 15:00 may confuse people. |
| Cache warm-up before a traffic peak | No | After the peak, the job has no value. |
| Every-minute health probe | No | The next run is seconds away. |
A useful rule: catch up jobs whose purpose is "this must happen about once per period". Skip catch-up for jobs whose value depends on the exact time.
Persistent timers in systemd
A systemd timer unit can remember when it last ran and compare that with its schedule. The setting is Persistent=, and it applies only to timers that use OnCalendar=.
# /etc/systemd/system/backup.timer
[Unit]
Description=Run the nightly backup
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=5min
[Install]
WantedBy=timers.targetWith Persistent=true, systemd stores the last trigger time of the timer on disk. For a timer in the system manager, the stamp file lives in /var/lib/systemd/timers/, named stamp-backup.timer. When the timer unit starts, for example at boot, systemd checks whether at least one calendar event has passed since that stamp. If so, it triggers the service immediately.
What "catch up" means exactly
Persistent catches up once. If the machine was off for a week and the timer is daily, you get one run at boot, not seven. This is usually what you want, because a job that is idempotent produces the same result whether it runs once or seven times (see the chapter on idempotent jobs).
The catch-up run happens when the timer activates. That is often at boot, and many other services are starting at the same time. Two settings help:
RandomizedDelaySec=adds a random delay up to the given value to each trigger. On a fleet of servers that all boot together, this avoids a thundering herd.- Ordering on the service, such as
After=network-online.targetandWants=network-online.targetin the[Unit]section of the service, ensures a job that needs the network does not start before it is available.
Checking the stamp and the timer state
systemctl list-timers backup.timer
ls -l /var/lib/systemd/timers/
systemctl status backup.timerlist-timers shows the NEXT and LAST columns. After a catch-up run, LAST shows the time of the catch-up run, not the time the job should have run.
To test catch-up behavior safely, stop the timer, delete the stamp, and start it again:
sudo systemctl stop backup.timer
sudo rm /var/lib/systemd/timers/stamp-backup.timer
sudo systemctl start backup.timer
journalctl -u backup.service --since "5 minutes ago"With no stamp file, systemd has no record of a previous run. Behavior in this case differs between versions, so test on your own system instead of assuming. The more reliable test is to set the system's timer to a short interval, such as OnCalendar=*:0/2, stop the timer for several minutes, and start it again. A run should appear right after the start.
Persistent does not apply to monotonic timers
OnBootSec=, OnUnitActiveSec=, and similar options are monotonic timers. They are relative to an event, not to the calendar. They do not use Persistent=. A timer such as OnUnitActiveSec=1d runs the job one day after it last became active. If the machine was off, that clock simply restarts at the next boot. A common pattern for "run once shortly after boot, then daily" is:
[Timer]
OnBootSec=15min
OnUnitActiveSec=1dThis does not guarantee that a run happens each calendar day. It guarantees spacing between runs while the system is up.
Failure modes
- The service fails during the catch-up run. The stamp is updated when the timer triggers, not when the service succeeds. A failed run is not retried at the next boot. You need your own retry logic or monitoring (see the chapters on retries and heartbeats).
- The timer is disabled. A timer that is not enabled does not start at boot, so it never evaluates the stamp. Check with
systemctl is-enabled backup.timer. - The stamp directory is not preserved. On systems with a read-only or volatile
/var, or in images that are reset on reboot, the stamp is lost andPersistent=has no effect. - A very long outage with a time jump. If the clock is wrong at boot and corrected later, the first trigger may be based on bad time. Keep time synchronization (chrony or systemd-timesyncd) running early in boot.
anacron for cron-based systems
anacron is the cron-world answer to missed runs. It does not run at a specific time of day. It runs jobs with a period measured in days, and records the date each job last completed. When anacron starts, it runs any job whose period has elapsed.
Anacron is typically started at boot and also invoked from cron or a systemd timer. On Debian and Ubuntu, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly are run through anacron when it is installed, so those scripts survive downtime.
The anacrontab format
The file is /etc/anacrontab. Each job line has four fields: period in days, delay in minutes, a job identifier, and the command.
# /etc/anacrontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
[email protected]
START_HOURS_RANGE=3-22
# period delay job-id command
1 10 nightly-backup /usr/local/bin/backup.sh
7 25 weekly-report /usr/local/bin/report.sh
@monthly 45 monthly-cleanup /usr/local/bin/cleanup.sh- period is the number of days between runs. Macros
@daily,@weekly, and@monthlyare accepted. - delay is how many minutes anacron waits after it starts before running this job. Use different delays so jobs do not all start at once.
- job-id is the name used for the timestamp file. Keep it unique and without spaces.
START_HOURS_RANGErestricts the hours in which jobs may start, so a catch-up run does not begin in the middle of business hours.
Timestamps are stored in /var/spool/anacron/, one file per job-id, containing the date of the last run as YYYYMMDD.
How anacron differs from Persistent timers
| Aspect | systemd `Persistent=true` | anacron |
|---|---|---|
| Time resolution | Any calendar expression, down to the second | Whole days (one day minimum) |
| Time of day | Fixed, with catch-up if missed | Not fixed. Runs after start, within allowed hours |
| State | Stamp file per timer | Date file per job |
| Granularity of "missed" | Any scheduled event | Period in days elapsed |
| Typical use | Per-job timers | Daily, weekly, monthly maintenance |
Anacron cannot express "every 15 minutes" or "at 02:30 exactly". If you need a precise time of day and also catch-up, use a systemd timer with Persistent=true.
Testing anacron
sudo anacron -T # test the syntax of /etc/anacrontab
sudo anacron -f -n -d # force, run now, stay in the foreground
cat /var/spool/anacron/nightly-backup-T validates the file. -f ignores the timestamps and -n runs jobs now without waiting for their delay. -d keeps anacron in the foreground and logs to standard error. Do this in a test environment first, because -f really runs every job.
Making jobs safe for catch-up runs
A catch-up run can happen at an unusual time, such as right after boot while the disk is busy. Keep these properties in the job itself:
- Idempotent. Running twice must be as safe as running once. Write results to a temporary file and rename it into place.
- Time-independent. Do not compute "yesterday" from a fixed assumption about when the job runs. If the script backs up "today's" data, a late run may capture the wrong day. Use an explicit date in the output name, or compute the target period from the data.
- Lock-protected. A catch-up run at boot could overlap with a regular run if the machine comes up near the scheduled time. Use
flock, as in the earlier chapter. - Dependency-aware. A job that needs a database or a mount must wait for it. Use
After=andRequires=in the unit, or retry for a bounded time in the script.
Detecting runs that were still missed
Catch-up helps only when the scheduler is running. A heartbeat check covers every other case, including an unenabled timer, a lost stamp file, and a broken script. The script ends with a request to a monitoring URL, and the monitor alerts when no request arrives within the expected period plus a grace time. For a daily job, set the period to 24 hours and a grace time of a few hours, longer than RandomizedDelaySec and the longest plausible catch-up delay.
Summary of decisions
- Use
Persistent=trueon everyOnCalendar=timer whose job should happen once per period regardless of downtime. - Use
anacronwhen you stay with cron and the job period is one or more days. - Do not rely on catch-up for jobs tied to a precise moment.
- Always pair catch-up with idempotent jobs, locks, and a heartbeat.
Exercise
A daily systemd timer for a database dump uses OnCalendar=*-*-* 03:00:00 without Persistent=. The server is powered off from 01:00 to 09:00 for three nights in a row. How many dumps are made during those three nights, and what single change fixes it? What happens if you add that change and the server is then off for a whole week?
Show answer
No dumps are made, because the 03:00 moment passes while the server is off and a timer without Persistent does not catch up. Add Persistent=true to the [Timer] section. After a week of downtime, the timer triggers once when it starts, not seven times, so the job should be idempotent and one run must be enough to bring the data up to date.
Exercise
You have an /etc/anacrontab line 1 10 nightly-backup /usr/local/bin/backup.sh. The machine was last up on 2026-11-26 and the job's timestamp file contains 20261126. It boots on 2026-11-28 at 04:20. What does anacron do, and what limits when it can start the job?
Show answer
One day or more has passed since 20261126, so the period has elapsed. Anacron waits the 10-minute delay and then runs the job once, not once per missed day. If START_HOURS_RANGE is set, for example 3-22, anacron starts the job only within those hours, and 04:30 falls inside that range.