systemd Timers as a Modern Alternative to Cron

Create, enable and check a systemd timer on Linux, and see how its schedule, logging and missed-run handling differ from a crontab entry.

Guides Published 4 min read

Contents

A systemd timer is a unit that starts another unit, usually a service, on a schedule. This guide shows how to turn a cron-style job into a timer plus a service, enable it, and check when it runs.

How a systemd timer differs from a crontab entry

A crontab line holds the schedule and the command together. With systemd you split them: a .service unit describes what to run, and a .timer unit describes when. If you do not set Unit=, a timer named backup.timer starts backup.service.

Some practical differences, based on the man pages:

  • Schedule syntax. cron uses five fields (minute, hour, day of month, month, day of week). Timers use OnCalendar= expressions such as *-*-* 02:30:00, plus shorthands like daily and weekly. Timers can also be relative to boot or to the last run.
  • Missed runs. With Persistent=true, systemd saves the last trigger time on disk and starts the service on activation if a run was missed. The crontab(5) page does not describe catching up on missed jobs.
  • Output. By default, a service’s stdout goes to the journal, and stderr is inherited from stdout. cron, by contrast, mails output to the crontab owner unless MAILTO says otherwise.
  • Spreading load. RandomizedDelaySec= adds a random delay, and AccuracySec= (default one minute) lets systemd fire within a window.

Step 1: write the service

A job that runs to completion and exits fits Type=oneshot. With this type, you can list several ExecStart= lines, and they run in order. The script path below is an example; replace it with your own.

; /etc/systemd/system/report.service
[Unit]
Description=Write a daily report

[Service]
Type=oneshot
ExecStart=/usr/local/bin/make-report.sh

Do not add an [Install] section to this service. The timer is what you enable.

Step 2: write the timer

This timer runs the service every day at 02:30 and catches up if the machine was off at that time.

; /etc/systemd/system/report.timer
[Unit]
Description=Run report.service daily

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

[Install]
WantedBy=timers.target

The [Install] section is what systemctl enable uses to create the symlinks that start the timer.

Step 3: check the schedule before enabling

systemd-analyze calendar normalizes an expression and shows when it fires next:

systemd-analyze calendar "*-*-* 02:30:00"
systemd-analyze calendar weekly

The weekly shorthand is documented as Mon *-*-* 00:00:00.

Step 4: enable and start the timer

After creating or editing unit files, reload the manager. Then enable the timer with --now, which also starts it. The enable command alone does not start units.

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

Step 5: see when it runs and what it printed

systemctl list-timers shows each timer with its next and last run and the service it activates.

systemctl list-timers
systemctl status report.timer
journalctl -u report.service

You can run the job right away to test the service itself, without waiting for the schedule:

sudo systemctl start report.service

Other schedule options

For jobs that should run relative to boot rather than the clock, use monotonic settings instead of OnCalendar=:

[Timer]
OnBootSec=15min
OnUnitActiveSec=1h

OnBootSec= counts from boot. OnUnitActiveSec= counts from the last time the service was activated. Monotonic timers pause while the system is suspended unless WakeSystem= is enabled. Persistent= applies only to OnCalendar=.

Per-user timers

Put the same two files in your own unit directory and use systemctl --user instead of sudo systemctl. The --user switch talks to your own service manager rather than the system-wide one.

Mapping a cron line

crontabtimer equivalent
30 2 * * *OnCalendar=*-*-* 02:30:00
@dailyOnCalendar=daily (midnight)
@rebootOnBootSec= with a short delay (it fires immediately if the time has already passed)

Timers are not strictly better. cron is simpler for a one-line job, and a timer needs two files. They pay off when you want journal logs or catch-up after downtime.

Sources

  1. systemd.timer(5)
  2. systemd.time(7)
  3. systemctl(1)
  4. systemd.service(5)
  5. systemd.exec(5)
  6. crontab(5)

#Systemd #Timers #Cron #Scheduling #Systemctl

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.