failover.io
  1. Home
  2. Guides
  3. systemd timer vs cron

systemd timer vs cron

What each one does better, the same job written both ways and OnCalendar next to cron.

Updated 19 November 20266 min read

Cron and systemd timers both run a command on a schedule. Cron is one line in a crontab and works the same on every Unix system. A systemd timer takes two small files, and in exchange you get logs in the journal, missed runs caught up after downtime, no overlapping runs and second level precision.

Side by side

cronsystemd timer
SetupOne crontab lineTwo unit files
Smallest step1 minute1 second
OutputMailed, or a file you redirect toIn the journal
Machine off at run timeThe run is skippedCan run at the next boot
Previous run still goingA second copy startsNothing starts until it ends
Random start delayBy hand, with sleepBuilt in
Time zoneThe system'sPer timer
CPU and memory limitsNoneAny systemd limit
Next run timesNot shownShown by list-timers

The same job both ways

A backup script every day at 3:00. With cron:

0 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

With systemd, first the service, in /etc/systemd/system/backup.service:

[Unit]
Description=Nightly backup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

Then the timer, with the same name, in /etc/systemd/system/backup.timer:

[Unit]
Description=Run the nightly backup

[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

[Install]
WantedBy=timers.target

Load and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer

Enable the .timer, not the .service. The timer starts the service with the same name; Unit= in the timer points it at another one. To run the job once by hand: sudo systemctl start backup.service.

OnCalendar next to cron

The format is weekday year-month-day hour:minute:second, and each part can be left out or written as *.

SchedulecronOnCalendar
Every 15 minutes*/15 * * * **:0/15
Every hour0 * * * *hourly
Every day at 3:000 3 * * **-*-* 03:00:00
Weekdays at 9:000 9 * * 1-5Mon..Fri 09:00
Mondays at midnight0 0 * * 1weekly
1st of the month0 0 1 * *monthly

A time zone goes at the end, like *-*-* 03:00:00 Europe/Paris. Cron has no per job time zone, except CRON_TZ on cronie.

Two traps. weekly in systemd is Monday, while @weekly in cron is Sunday. quarterly means every three months. To check an expression and see when it runs next:

systemd-analyze calendar "Mon..Fri 09:00"

For the cron side, the cron expression tester shows the next runs, and crontab syntax covers every field.

Timer options worth knowing

  • Persistent=true stores the last run on disk. If the machine was off when a run was due, the job runs right after the next boot.
  • AccuracySec=1s makes the job start on the second. The default of 1 minute lets systemd start it up to a minute late, to group wakeups.
  • RandomizedDelaySec=10min adds a random delay up to that value, so 50 servers don't all run the job at the same second.
  • OnBootSec=5min runs the job once, 5 minutes after boot.
  • OnUnitActiveSec=1h runs it 1 hour after the last start, wherever that falls on the clock. Combine it with OnBootSec= for a job that repeats from boot.
  • MemoryMax= and CPUQuota= in the .service cap what the job can use.

Check and debug

TaskCommand
Next and last run of every timersystemctl list-timers
State of one timersystemctl status backup.timer
Output of the jobjournalctl -u backup.service
Output since this morningjournalctl -u backup.service --since today
Find a failed runsystemctl --failed

The cron side of this is in cron logs and list cron jobs.

Which one to use

Keep cron when the job is simple, the machine is always on or the same crontab has to work on systems without systemd (Alpine and most containers, the BSDs, macOS). On Kubernetes, use a CronJob instead of either. Switch to a timer when a missed run matters after downtime, when a slow run must never overlap the next one, when you want the output in the journal with the rest of the system, or when the job needs a memory or CPU cap.

Get an alert when it stops

A failed run shows in systemctl --failed, but nobody gets told, and a timer that was never enabled after a rebuild shows nothing at all. Add a heartbeat ping in the service. ExecStartPost= only runs when ExecStart= succeeded:

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
ExecStartPost=curl -fsS -m 10 -X POST https://api.failover.io/heartbeat/<your-monitor-id>

With cron, the same ping goes after &&. When a ping is late, you get an alert. See how to monitor cron jobs.

Frequently asked questions

Are systemd timers better than cron?

For jobs on a server that runs systemd, usually: logs in the journal, missed runs caught up with Persistent=true, and no overlapping runs. Cron is simpler and works on systems without systemd.

How do I list systemd timers?

Run systemctl list-timers. Add --all to include inactive timers.

What is the systemd equivalent of a cron job every 5 minutes?

OnCalendar=*:0/5 in the timer. Check it with systemd-analyze calendar "*:0/5".

Does a systemd timer run a missed job after a reboot?

Only with Persistent=true in the [Timer] section, and only for OnCalendar= timers. The default is false.

Can a systemd timer run every 30 seconds?

Yes, with OnCalendar=*:*:0/30 and AccuracySec=1s. Without the second line, runs can start up to a minute late.