Skip to content

Lesson 6 of 9 · 24m

Timers Instead of Cron: Calendar Events, Accuracy, and Missed Runs

How systemd timers model schedules, randomized jitter, persistent catch-up, and service coupling — plus the cron habits that do not translate.

Cron survives because it is simple. Timers are better on systemd systems because the schedule, job, logs, dependencies, and failure state live in one supervision model. The catch is that timers are event generators, not scripts — design the service they trigger.

A real timer pair

# backup.service
[Unit]
Description=Nightly object storage backup

[Service]
Type=oneshot
ExecStart=/opt/backup/bin/backup-run --config /etc/backup/config.yaml
# backup.timer
[Unit]
Description=Run nightly object storage backup

[Timer]
OnCalendar=*-*-* 02:15:00
RandomizedDelaySec=15min
Persistent=true

[Install]
WantedBy=timers.target

Enable the timer with systemctl enable --now backup.timer — not the service. Manual runs use the same service and journal stream as scheduled runs.

Calendar syntax is expressive but exact

OnCalendar=Mon..Fri 09:30:00
OnCalendar=*:0/15

Test expressions before deployment:

systemd-analyze calendar 'Mon..Fri 09:30:00'

Missed runs, jitter, and precision

Persistent=true is the anacron-like behavior: if the machine was off when the timer should have fired, systemd catches up later. Correct for backups; wrong for jobs harmful when replayed late.

RandomizedDelaySec=15min spreads fleet-wide jobs so hundreds of servers do not hit shared infrastructure at the same second. AccuracySec= controls timer coalescing. If you truly need precision, set AccuracySec=1us, but understand that high-precision timers wake the machine more often.

Dependencies belong on the service

[Unit]
RequiresMountsFor=/var/lib/backup
After=network-online.target
Wants=network-online.target

A timer does not make networking, mounts, credentials, or secrets exist. Build jobs to be idempotent; use flock inside the job if overlap is possible through manual starts or unusual scheduling.

Field notes

  • backup.timer triggers backup.service by basename unless Unit= says otherwise.
  • Enabling a service does not enable its timer.
  • OnBootSec= and OnUnitActiveSec= are relative timers; combine them with calendar events deliberately.
  • Cron mailed output; systemd sends output to the journal. Alerting belongs in monitoring, not mailbox archaeology.

Was this lesson helpful?

Sign in to rate