Available: permanent, freelance, remoteMorocco

ENFR
Contact me
Topics

NewsSystems

Replacing cron with systemd timers

Write a timer, test its expression with systemd-analyze calendar, read every run in the journal and catch up a missed run.

El Mehdi Jouan

El Mehdi Jouan · Systems & support engineer

· 3 min read

A cron job that fails tells no one. Its output goes to a local mail nobody reads, and if the machine was off at the scheduled time, the job is simply skipped. systemd timers fix both: each run is logged like a service, and a missed run can be caught up at boot.

The tests were done on Ubuntu 26.04 (systemd 259), as user timers so as not to touch the system. For a system timer, use the same files in /etc/systemd/system/ and the same commands without --user. The files and commands are the same on RHEL 8 and Oracle Linux 8 (systemd 239); I checked the calendar expressions there, on Oracle Linux 8.10. One option is missing on those versions, noted below.

1. Two files: the service and the timer

The service says what to run. The timer says when.

# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
# /etc/systemd/system/backup.timer
[Unit]
Description=Backup every night

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=10min

[Install]
WantedBy=timers.target

The timer has the same name as the service: backup.timer triggers backup.service. Type=oneshot means the service does its job and then stops.

2. Test the schedule before writing it

This is the advantage I like most: an expression can be checked before it is used.

systemd-analyze calendar "*-*-* 02:30:00"
Normalized form: *-*-* 02:30:00
    Next elapse: Mon 2026-11-30 02:30:00 +00
       From now: 11h left

A few useful expressions, all checked with this command:

Mon..Fri 08:00          Monday to Friday at 8:00
*:0/15                  every 15 minutes
Mon *-*-1..7 03:00      the first Monday of the month at 3:00

To see the next runs of a repeating expression: systemd-analyze calendar --iterations=3 "*:0/15". The --iterations option does not exist yet on RHEL 8 and Oracle Linux 8: without it, the command gives the next run.

3. Enable and check

sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timer
NEXT                        LEFT LAST PASSED UNIT              ACTIVATES
Mon 2026-11-30 02:37:12 +00  11h -         - demo-backup.timer demo-backup.service

You enable the timer, not the service. The time shown is 02:37, not 02:30: that is RandomizedDelaySec=10min, which shifts the start by a random delay. Across a fleet of servers, it keeps every machine from hitting the same backup server in the same second.

systemd-analyze verify backup.timer reads the files and reports syntax errors; it says nothing when everything is correct.

4. Run it by hand and read the result

sudo systemctl start backup.service
journalctl -u backup.service -n 20
systemctl status backup.service

Starting the service by hand does not touch the timer: that is the right way to test. All the script's output is in the journal, with timestamps, and systemctl status gives the exit code of the last run. A failed backup also shows up in systemctl --failed, which a cron job never does.

5. Catch up a missed run

Persistent=true keeps the date of the last run on disk. If the machine was off at 02:30, the job runs at the next boot instead of waiting for the following night. The date is kept in /var/lib/systemd/timers/ (in my user-mode test: ~/.local/share/systemd/timers/stamp-demo-backup.timer).

Without this option, a laptop or a test machine switched off at night would never run its backup.

6. A one-off job, without writing a file

sudo systemd-run --on-active=30min /usr/local/bin/purge.sh

systemd creates a temporary timer that runs the command in 30 minutes. It shows in systemctl list-timers and disappears after it runs. This is what I use to schedule a rollback before a risky change (see the article on sshd_config).

What I keep from cron

Cron is still quicker to write for an unimportant job. As soon as a job needs watching (backup, purge, certificate renewal), I move it to a timer: you know when it ran, what it said, and whether it failed.

The procedure, in short

systemd-analyze calendar "*-*-* 02:30:00"
sudo systemctl enable --now backup.timer
systemctl list-timers
journalctl -u backup.service -n 20
  • linux
  • systemd
  • timer
  • cron
  • OnCalendar