Skip to content

Cron vs systemd timers — when to pick which

A decision guide between cron and systemd timers — logging, dependency handling, missed runs, randomization, and the failure modes each one hides.

You have a recurring task to schedule on a Linux host — a backup, a certificate renewal, a log rotation, a polling job. Cron is the obvious answer, but systemd ships with a timer subsystem that does almost the same thing differently. This article walks the real differences — not the “cron is old, systemd is new” headlines, but the operational consequences in logging, missed-run recovery, dependency chains, and security boundaries.

How to verify

For any host, you can see both scheduling worlds at once:

crontab -l
sudo ls /etc/cron.d/ /etc/cron.hourly/ /etc/cron.daily/ /etc/cron.weekly/
systemctl list-timers --all
systemctl status <name>.timer
journalctl -u <name>.service --since=yesterday

list-timers shows everything the systemd timer subsystem knows about, plus the last and next run. journalctl -u is the equivalent of “look for the cron output that was emailed to root” — except it’s structured, searchable, and not lost when the MTA breaks.

What’s happening

Cron is a daemon (vixie-cron, cronie, dcron) that reads crontab files, parses time specs, and spawns processes via fork+exec at the scheduled moment. It runs as root, executes user jobs under the right uid, and emails output to the job owner via a local MTA. The execution context is whatever cron’s environment is — a minimal PATH, no LANG, no HOME for most users.

A systemd timer is a unit (<name>.timer) that activates another unit (typically <name>.service) on a schedule. Activation happens via systemd, so the service inherits the unit’s full configuration — resource limits, namespaces, capability dropping, working directory, environment files, and dependencies on other units (network online, mount points, other services). Output goes to the journal automatically and tags include the unit name. OnCalendar= accepts the same syntax cron uses plus more flexible expressions; OnBootSec= and OnUnitActiveSec= enable schedules cron can’t express.

Decision

For a brand-new task on a modern Linux host (systemd-based: Ubuntu, Debian, RHEL/Alma, SUSE), use a systemd timer. Reasons:

  • Logging. The job output is in journalctl -u <name>.service, structured, with severity, and integrated with journalctl --since queries. Cron’s “mail to root” only works if a local MTA is configured and someone reads the mail.
  • Missed runs. Persistent=true on the timer causes systemd to run the job after a reboot if it would have run while the system was down. Cron has no equivalent without anacron, which is a separate package with separate configuration.
  • Resource limits. A service unit can set MemoryMax=, CPUQuota=, and OOMScoreAdjust= so a runaway job does not take the host with it. The cron job runs with the inherited limits of the cron daemon.
  • Dependencies. After=network-online.target ensures the job does not start until the network is up. Cron has no graph; if the network is not up yet, the job fails or hangs and there is no clean retry.
  • Randomization. RandomizedDelaySec= jitters the start to spread load across a fleet. Cron can do this only with a sleep $((RANDOM % N)) hack inside the job.
  • Security. The unit can set PrivateTmp=true, ProtectSystem=strict, NoNewPrivileges=true, and a restricted system call set. Cron jobs inherit none of this.

Pick cron when the host doesn’t run systemd (BSD, Alpine without OpenRC, embedded), when the job is being shared with operators who know cron and not systemd, or when you genuinely need cron’s per-user crontab pattern (rare). Don’t pick cron because “I already know it” — that’s how production gets a system clock skew bug nobody can debug.

The procedure

Convert a cron job to a systemd timer. Here is the canonical pair:

# /etc/systemd/system/backup-db.service
[Unit]
Description=Daily Postgres logical backup
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
User=postgres
ExecStart=/usr/local/bin/backup-postgres.sh
Nice=10
IOSchedulingClass=best-effort
IOSchedulingPriority=7
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/var/backups/postgres
NoNewPrivileges=true
TimeoutStartSec=4h
# /etc/systemd/system/backup-db.timer
[Unit]
Description=Run Postgres backup nightly
[Timer]
OnCalendar=*-*-* 02:30:00
RandomizedDelaySec=900
Persistent=true
AccuracySec=1min
[Install]
WantedBy=timers.target

Activate and verify:

sudo systemctl daemon-reload
sudo systemctl enable --now backup-db.timer
systemctl list-timers backup-db.timer
sudo systemctl start backup-db.service   # manual test run
journalctl -u backup-db.service -f

Once the timer is running, remove the old cron entry — do not leave both in place.

Common pitfalls

  • A timer with OnCalendar= but no matching service will silently do nothing — list-timers shows it, but it never fires anything.
  • Persistent=true runs missed jobs at boot — fine for backups, very wrong for a job that must skip when it cannot run on time.
  • User= on the service runs the command as that user but the working directory is /; set WorkingDirectory= if the script assumes a home directory.
  • The cron MAILTO= is genuinely useful for legacy infra — under systemd you replicate it with a journal-to-email shipper or alerts on OnFailure= units.
  • RandomizedDelaySec= jitters the start time only; back-to-back runs on the same host can still overlap if a long run pushes into the next slot — set a sensible TimeoutStartSec= to bound it.

In the engagements we run, systemd timers are the default scheduler on every Linux host we support — units under Git, output in the journal, missed runs replayed on boot, and OnFailure units that page the support desk. Cron remains in legacy estates we have not yet migrated. See /en/services/managed-operations/.