systemd OnCalendar vs POSIX Cron: AND Logic, Reverse Offsets (~), and Persistent=true
Avoid critical translation bugs between systemd.timer OnCalendar expressions and 5-field POSIX cron, including DOM+DOW intersection, seconds, and missed boot catch-up.
Incident Symptom & Protocol Root Cause
Production Failure Symptom
Porting a systemd timer like OnCalendar=Fri *-*-13 09:00:00 to crontab as 0 9 13 * 5 causes the job to fire every single Friday, or a migrated systemd timer fires up to 60 seconds 'late' due to default AccuracySec=1min coalescing.
Scheduler Parser & Clock Mechanics
systemd calendar expressions use boolean AND across weekday and day-of-month (the exact opposite of POSIX cron's OR union), include a mandatory or defaulted seconds component, coalesce wakeups within AccuracySec=1min to save CPU power, and support reverse month-end (~) offsets that have no 5-field cron token.
Broken vs Verified Configuration
# Source systemd unit (/etc/systemd/system/friday13.timer):
# [Timer]
# OnCalendar=Fri *-*-13 09:00:00
# BROKEN port to /etc/crontab (changes AND intersection into OR union!):
0 9 13 * 5 root /usr/local/sbin/audit-snapshot
# Also surprising in systemd: timer appears to drift up to 60s without AccuracySec!# 1. Exact POSIX crontab equivalent of OnCalendar=Fri *-*-13 09:00:00:
0 9 13 * * root [ "$(date +%u)" = "5" ] && /usr/local/sbin/audit-snapshot
# 2. Production-grade systemd timer with deterministic second precision & boot catch-up:
[Unit]
Description=Deterministic Audit Snapshot Timer
[Timer]
OnCalendar=Fri *-*-13 09:00:00 UTC
AccuracySec=1s
RandomizedDelaySec=0
Persistent=true
[Install]
WantedBy=timers.targetOperational Deep Dive
systemd timers solve four chronic limitations of classic Vixie crond on Linux servers: every run executes inside its own isolated cgroup with structured journald logging (journalctl -u myjob.service), boolean calendar matching uses intuitive AND intersection (Mon *-*-01..07 09:00:00 means the first Monday of the month), reverse day offsets (*-*-~01) express month-end dates natively, and Persistent=true records the last trigger timestamp on disk (/var/lib/systemd/timers/) so powered-off hosts catch up immediately on boot (replacing both cron and anacron).
Two systemd defaults regularly surprise engineers coming from crond. First, systemd defaults to AccuracySec=1min so the kernel can batch timer wakeups across background services for power efficiency—meaning OnCalendar=*-*-* 09:00:00 may legitimately start anywhere between 09:00:00 and 09:01:00 unless you set AccuracySec=1s! Second, writing OnCalendar=*-*-* *:00/15 (omitting the seconds field) defaults seconds to :00, whereas writing *:*:00/15 fires every 15 seconds!
Before deploying or porting any systemd schedule, validate its exact next N iterations locally using systemd-analyze calendar --iterations=5 'Mon *-*-01..07 09:00:00 UTC'. Unlike crontab files that only reveal typos at runtime, systemd-analyze calendar parses timezones, DST transitions, and tilde offsets deterministically offline.
systemd.timer vs POSIX Crontab Feature & Translation Matrix
| Engine / Case | Runtime Behavior | Production Remediation |
|---|---|---|
| Weekday + Day-of-Month (Mon + 01..07) | systemd uses boolean AND (first Monday only); POSIX cron uses boolean OR (days 1-7 plus all Mondays). | Add [ "$(date +\%u)" = "1" ] && guard when translating systemd AND rules to POSIX cron. |
| Sub-minute start precision | systemd coalesces wakeups within a default AccuracySec=1min window (up to 60s delay). | Set AccuracySec=1s in [Timer] when exact top-of-minute trigger timing is required. |
| Missed schedule while server powered off | Cron skips missed downtime runs; systemd with Persistent=true triggers once immediately at boot. | Enable Persistent=true for daily backups/rotations; leave false for time-sensitive market hooks. |
| Concurrent overlapping runs | Cron spawns duplicate processes every tick; systemd .service units are singletons by default! | A running oneshot/simple systemd service will NOT start a second overlapping instance when its timer re-fires. |
Edge Cases & Engineering FAQs
Can a systemd timer spawn overlapping duplicate instances like crontab does?
No! Because a systemd .timer activates a single corresponding .service unit (unless using @ template instances), if myjob.service is still active when the next timer elapses, systemd sees the unit is already running and coalesces/skips duplicate activation automatically—providing built-in singleton lock semantics without flock.
How do I add controlled jitter to a fleet of 500 Linux VMs running systemd timers?
Add RandomizedDelaySec=10m alongside FixedRandomDelay=true inside [Timer]. FixedRandomDelay hashes the machine-id so each VM gets a deterministic, repeatable offset inside the 10-minute window rather than hammering your artifact mirror at 00:00:00.
How do I specify an IANA timezone inside OnCalendar?
Append the IANA zone name at the end of the expression, for example OnCalendar=Mon..Fri *-*-* 09:30:00 America/New_York, and verify with systemd-analyze calendar.
Runtime Dead-Man's-Switch Verification
Dead-man's-switch heartbeat checks that page on-call when a scheduled job skips a DST gap, stalls on a lock, or exits non-zero.