Systemd has timers, which can be used as an alternative to cron to schedule jobs. The timers infrastructure is quite powerful and is described in detail elsewhere. ⁽¹⁾ ⁽²⁾
The following shows common timer examples to execute an arbitrary service unit myunit and basic commands to control the timers.
Monotonic timer
[Unit]
Description=Run myunit.service every 60 minutes
[Timer]
OnBootSec=15min
OnUnitActiveSec=60min
Unit=myunit.service
[Install]
WantedBy=timers.target
{: file=“myunit.timer” }
Realtime timer
[Unit]
Description=Run myunit.service once a week at 2:00 AM
[Timer]
OnCalendar=Tue *-*-* 02:00:00
Unit=myunit.service
[Install]
WantedBy=timers.target
{: file=“myunit.timer” }
Launch and monitor the timer
Timers can be added as a regular user or system-wide as root.
-
Add the timer unit file
$ systemctl edit --full myunit.timer
-
Enable the timer (not the service unit)
$ systemctl enable myunit.timer
-
Monitor the active timers
$ systemctl list-timers
Timers can be manually triggered with the command
systemctl start myunit.timer
. {: .prompt-info }