Skip to content

Lesson 3 of 14 · 15m

Replacing cron with systemd timers

Timer+service pairs that log to the journal, fire missed runs at boot, and carry every sandboxing directive.

Every cron job on a production box shares three failure modes: nobody sees its output, nobody knows it failed, and the one person who could read its local mail stopped reading local mail in 2009. The wrong mental model is "cron is traditional and fine." It's fine for a Raspberry Pi watering plants. On a server you're paid to keep alive, a scheduled job that can't log, can't order itself after a dependency, and can't be sandboxed is technical debt with a crontab.

The mechanism

A timer is a unit pair — backup.timer says when, backup.service says what:

# /etc/systemd/system/backup.timer
[Unit]
Description=Nightly offsite backup

[Timer]
OnCalendar=02:30
Persistent=true
RandomizedDelaySec=300

[Install]
WantedBy=timers.target
# /etc/systemd/system/backup.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/offsite-backup

OnCalendar= takes real calendar specs — Mon..Fri 03:00, *-*-* 04:15, weekly — and systemd-analyze calendar "Mon 02:30" prints the next firings in plain English before you commit. Persistent=true is the feature cron doesn't have: a box powered off at the scheduled time fires the job at next boot, so the missed 2 a.m. backup doesn't become next week's incident. RandomizedDelaySec=300 spreads a fleet's jobs instead of hundreds of boxes hitting your backup endpoint in the same second.

systemctl enable --now backup.timer
systemctl list-timers --all      # next fire, last fire, and what ran
journalctl -u backup.service     # output lands in the journal like everything else

Output goes to the journal — searchable, shippable to your collector, and attached to a unit that can carry Environment=, User=, and every sandboxing directive from the earlier lessons. Cron mails output to a spool nobody reads, or drops it entirely.

Interval jobs too

OnCalendar= is wall-clock scheduling. For "every ten minutes" jobs, the monotonic timers count from events instead: OnBootSec= (after boot), OnUnitActiveSec= (after the service last finished running — the honest version of */10 * * * *), OnUnitInactiveSec=:

[Timer]
OnBootSec=2min
OnUnitActiveSec=10min

That pair is "run forever, every ten minutes" — except the timer version knows when the last run ended, so a slow job can't pile onto itself the way a cron that overruns does. AccuracySec=1min lets systemd coalesce nearby timers for power efficiency; tighten it when the schedule actually matters.

Cron's actual failure modes on servers

  • Minimal environment: cron runs with SHELL=/bin/sh and PATH=/usr/bin:/bin — the script that works in your SSH shell dies in cron because aws isn't in /usr/bin.
  • % is a newline in crontab command fields — date +%F silently truncates the command. Escape it (\%) or put it in a script file.
  • No missed-run recovery: box off at 3 a.m. means the job skipped, with no record it ever tried.
  • @reboot lines that race services which aren't up yet.

You don't have to rip out /etc/cron.d today — but every new scheduled job earns a timer, and the crons that matter (backups, renewals, report jobs) get converted first.

Try this

Convert one real cron entry into a timer+service pair. Run systemd-analyze calendar on your OnCalendar= before enabling — not after. systemctl start backup.service by hand, read journalctl -u backup.service, then power a lab VM off past the scheduled time and watch Persistent=true fire the job at boot.

Pitfalls

  • OnCalendar= typos fail at systemd-analyze — check before enabling.
  • Ordering goes on the service, not the timer: After=postgresql.service belongs in backup.service's [Unit].
  • A timer that fires while the previous run still runs: Type=oneshot plus flock inside the script, or overlapping runs corrupt each other.
  • Per-user crontabs (crontab -u deploy -l) hide jobs from crontab -l as root — enumerate every user's crontab before declaring "no cron on this box."

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.