LLI Wiki — My Digital Knowledge Base

systemd timers: basic commands

List, check and test systemd timers, with an example service and timer.

systemd timers schedule the execution of a service and can replace cron. This article summarises the basic commands to list and test timers, with an example service and timer.

List timers

Command Effect
sudo systemctl list-timers Lists active timers
sudo systemctl list-timers --all Lists all timers, including inactive ones
sudo systemctl list-timers --state=STATE Filters timers by state (replace STATE)

Check a calendar expression

sudo systemd-analyze calendar "Tue,Sun *-*-* 01,03:00:00"

This command shows how systemd interprets an OnCalendar expression before you use it in a timer.

Example: service and timer

Service file, /lib/systemd/system/backup-example.service:

[Unit]
Description=Custom backup (example)
[Service]
Type=simple
ExecStart=/bin/sh -cv '/root/exec_backup.sh'
Restart=on-failure
RestartSec=30s

Timer file, /lib/systemd/system/backup-example.timer:

[Unit]
Description=Daily timer for the backup-example service
[Timer]
OnCalendar=*-*-* 22:05:00
Persistent=true
[Install]
WantedBy=timers.target

Persistent=true tells systemd to catch up on a missed run, for example if the machine was powered off at the scheduled time.

Reload the configuration, then test the service:

sudo systemctl daemon-reload
systemctl start backup-example.service

Follow the service logs:

sudo journalctl -u backup-example -f

Debug mode

To diagnose a problem with systemctl, you can trace system calls with strace:

strace -v -f -s 256 -o /tmp/strace.log systemctl disable <service>

The systemd log level can also be raised temporarily. Note the current level first, then restore it:

systemctl log-level              # show the current level
systemctl log-level debug
systemctl daemon-reload
systemctl log-level info         # restore the original level

Notes

Mastodon LLIMastodon SocialFosstodon