Sub topic 11
Missed Runs: Persistent Timers and Anacron
Assume you can edit systemd unit files, use systemctl and journalctl, and understand basic cron syntax. This chapter focuses on what happens when a machine is powered off or a scheduler is stopped during a scheduled time.
Why scheduled runs are missed
A normal cron entry is checked only while the cron daemon is running. If the server is off at 02:30, cron does not normally remember that one run was missed. The next scheduled run happens at the next matching time.
That behavior is correct for some tasks. For example, a status report for 02:30 may be useless at 03:00. It is unsafe for other tasks, such as a daily backup or cleanup job that must run once during every day.
Choose a catch-up policy before writing the schedule:
| Policy | Suitable when | Typical mechanism |
|---|---|---|
| Skip the missed run | The exact scheduled time matters | Regular cron or a non-persistent timer |
| Run once after startup | Each period needs one attempt | `Persistent=true` or anacron |
| Process every missed period | Each interval represents required work | A queue or application-level schedule |
A catch-up mechanism usually performs one run, not one run for every missed calendar occurrence. If a laptop is off for seven days, it normally does not create seven backup processes automatically.
Persistent systemd timers
A systemd timer can record when it last triggered its associated service. With Persistent=true, systemd starts the service when the timer becomes active if a calendar run was missed while the timer was inactive.
The timer and service must have matching names. For example, backup.timer activates backup.service.
# /etc/systemd/system/backup.service
[Unit]
Description=Daily backup
[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/backup.sh# /etc/systemd/system/backup.timer
[Unit]
Description=Run the daily backup
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.targetInstall and inspect the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timer
sudo journalctl -u backup.servicePersistent=true applies when the timer was inactive, including when the system was shut down. It does not guarantee recovery from every failure. If the service starts and fails, systemd does not automatically repeat it unless you configure a retry policy or the job itself handles retries.
A persistent timer is useful on servers that sometimes reboot. It is less suitable when running late would cause harm, such as sending a time-sensitive notification.
Anacron for periodic work
Anacron is designed for jobs that should run daily, weekly, or monthly on machines that may be powered off. It stores timestamps to determine whether a period has elapsed. When the machine is available, anacron runs overdue jobs, possibly after a configured delay.
Anacron does not provide precise clock scheduling. Use it when “once per day” matters more than “at exactly 02:30.” On a continuously available server, a systemd timer usually gives clearer service management and logging.
# /etc/anacrontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
1 5 daily-backup /usr/local/bin/backup.sh
7 10 weekly-report /usr/local/bin/report.shThe fields mean period in days, delay in minutes, an identifier, and the command. Anacron runs a job at most once during its period. Test the command separately, then inspect the system's anacron logs after enabling it.
> callout warning > Do not assume that a missed run was successful. A catch-up mechanism only starts the job. Record exit status and monitor the result.
Testing a catch-up policy
Test recovery deliberately in a non-production environment. Stop the timer, move the system clock only in an isolated test environment, or use a short test interval. Confirm which service runs, whether it runs once, and where its logs appear.
Exercise
A daily backup timer has Persistent=true, but the server was off for three days. How many automatic backup service activations should you normally expect when the timer starts?
Show answer
One activation. Persistent timers catch up a missed calendar run rather than replaying every missed occurrence.