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-backupOnCalendar= 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 elseOutput 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=10minThat 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/shandPATH=/usr/bin:/bin— the script that works in your SSH shell dies in cron becauseawsisn't in/usr/bin. %is a newline in crontab command fields —date +%Fsilently 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.
@rebootlines 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 atsystemd-analyze— check before enabling.- Ordering goes on the service, not the timer:
After=postgresql.servicebelongs inbackup.service's[Unit]. - A timer that fires while the previous run still runs:
Type=oneshotplusflockinside the script, or overlapping runs corrupt each other. - Per-user crontabs (
crontab -u deploy -l) hide jobs fromcrontab -las root — enumerate every user's crontab before declaring "no cron on this box."
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Self-Host OpenClaw: VPS Setup to Production (2026)
Self-host OpenClaw from zero to production. Server provisioning, SSH lockdown, Docker, reverse proxy, Fail2ban, performance tuning, and full backup strategy in one guide.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.
Red Hat Called This Bug a 5.9. Two Weeks Later It Was a 9.8.
CVE-2026-98432 is a use-after-free in the IPv4 TCP handshake, reachable by anybody who can reach a listening port, sitting in the kernel since 4.4. Red Hat scored it 5.9 moderate and deferred the fix. Then 8.1. Then 9.8.
Self-Hosting Vaultwarden Without Trusting the Defaults
Vaultwarden gives you a Bitwarden-compatible password server in one container and about ten minutes. The defaults that make that possible leave org creation open, audit logging off, accounts on PBKDF2, and a rate limiter that trusts a header anyone can set.