Skip to content

Timers

Timers

wiki.archlinux.org/title/Systemd/Timers
https://systemd-by-example.com/

Making a Timer with a Service

Run 15mins after boot and then every week

Monotonic timer from a Service:

>>> cat /etc/systemd/system/acme_update.service
# This service unit is for testing timer units
# By David Both
# Licensed under GPL V2
#

[Unit]
Description=Logs system statistics to the systemd journal
Wants=myMonitor.timer

[Service]
Type=oneshot
ExecStart=/usr/bin/free

[Install]
WantedBy=multi-user.target
>>> cat /etc/systemd/system/acme_update.timer
[Unit]
Description=Logs some system statistics to the systemd journal
Requires=myMonitor.service

[Timer]
Unit=myMonitor.service
OnCalendar=*-*-* *:*:00

[Install]
WantedBy=timers.target

Still run the command immediately after boot if the computer was turned off during that time.

Realtime timer:

>>> cat /etc/systemd/system/acme_letsencrypt.timer
[Unit]
Description=Weekly renewal of Let's Encrypt's certificates

[Timer]
OnCalendar=weekly
RandomizedDelaySec=3h
Persistent=true

[Install]
WantedBy=timers.target

Running a Timer

Run a Timer:

sudo systemctl start <timer-unit-name>.timer

Testing that the Timer ran:

sudo systemctl status <timer-unit-name>.timer

Make Timer without service

Run in 30 seconds:

systemd-run --on-active=30 /bin/touch /tmp/foo

Run in 30 seconds after boot:

systemd-run --on-boot=30 /bin/touch /tmp/foo

Run every week:

systemd-run --on-calendar=weekly /bin/touch /tmp/foo