appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

systemd timers

Lesson 4 of 510 minBeginner

The modern alternative to cron on most Linux distributions, with better logs and control.

On systems that use systemd, a timer unit starts a service unit on a schedule. Compared with cron you get logging in the journal, dependencies, resource limits and the option to catch up on runs missed while the machine was off.

The service

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

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

The timer

/etc/systemd/system/backup.timer
[Unit]
Description=Run backup every night

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

[Install]
WantedBy=timers.target
  • OnCalendar is the schedule. Check an expression with systemd-analyze calendar "*-*-* 02:30:00".
  • Persistent=true runs a missed job at the next boot.
  • The timer and the service share the name by default (backup.timer starts backup.service).

Enable and inspect

sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers
journalctl -u backup.service

NoteCron is still fine for simple jobs and exists everywhere. Choose timers when you want logs, ordering or catch-up.

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

1. What does Persistent=true do in a timer?
2. Where do the output and errors of a timer-started service go by default?
HintA systemctl subcommand that lists timers.

Key terms