# 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.

- **Canonical URL**: https://opslint.dev/cron/systemd-oncalendar-to-cron-and-logic-differences/
- **Category**: `CRON`
- **Interactive Workbench**: [Open in Live Workbench](https://opslint.dev/cron/#expr=Fri%20*-*-13%2009%3A00%3A00&tz=UTC&dialect=systemd)
- **Normative Specifications**: `systemd.time(7) Calendar Events Specification`, `systemd.timer(5) Persistent=, RandomizedDelaySec=, AccuracySec=`, `POSIX.1-2017 crontab Utility`

## Incident Symptom & Root Cause

**Production 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.

**Protocol / Engine Root Cause**: 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.

## Remediation Diff (Broken vs Verified Fix)

### Broken Naive Port Between systemd.timer and POSIX Cron

```text
# 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!
```

### Exact Bidirectional systemd.timer & POSIX Equivalent

```text
# 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.target
```

## Engineering 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

| Case / Engine | Protocol Behavior | Production Remediation |
| :--- | :--- | :--- |
| **Weekday + Day-of-Month (Mon + 01..07)** (`#systemd-vs-cron-dom-dow`) | 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-accuracy-sec`) | 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** (`#systemd-persistent-catchup`) | 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** (`#systemd-overlap-protection`) | 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. |

## Frequently Asked Engineering Questions

### 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.

## Related Problem-Cluster Guides

- [The POSIX Cron Day-of-Month + Day-of-Week OR Trap (0 9 13 * 5 Runs Every Friday)](https://opslint.dev/cron/posix-dom-dow-or-boolean-trap/) — Why specifying both Day-of-Month and Day-of-Week in POSIX crontab evaluates as boolean OR (union) instead of AND, and how to guard intersection schedules.
- [Run a Cron Job on the Last Day of Every Month (28, 29, 30, and 31 Leap-Year Safe)](https://opslint.dev/cron/last-day-of-month-leap-year-cron-patterns/) — Portable month-end cron patterns across POSIX Linux/BSD, Kubernetes CronJob, GitHub Actions, AWS EventBridge (L), and systemd (~01) including February 29.
- [Prevent Overlapping Cron Jobs with Linux flock -n Non-Blocking Locks](https://opslint.dev/cron/flock-prevent-overlapping-cron-flock-wrappers/) — Stop runaway duplicate cron processes and database lock pileups using kernel-managed util-linux flock -n file descriptors instead of brittle PID files.
- [Fix Cron Jobs That Work in Terminal SSH But Fail Silently in Crontab (PATH, SHELL, Env)](https://opslint.dev/cron/cron-environment-path-locale-silent-failures/) — Diagnose why cron runs with minimal PATH=/usr/bin:/bin, /bin/sh (dash) instead of bash, missing SSH/cloud credentials, and C locale.
