Home Cron jobs with systemd
Post
Cancel

Cron jobs with systemd

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

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Run myunit.service every 60 minutes

[Timer]
OnBootSec=15min
OnUnitActiveSec=60min
Unit=myunit.service

[Install]
WantedBy=timers.target

Realtime timer

1
2
3
4
5
6
7
8
9
[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

Launch and monitor the timer

Timers can be added as a regular user or system-wide as root.

  1. Add the timer unit file

    1
    
     $ systemctl edit --full myunit.timer
    
  2. Enable the timer (not the service unit)

    1
    
     $ systemctl enable myunit.timer
    
  3. Monitor the active timers

    1
    
     $ systemctl list-timers
    

Timers can be manually triggered with the command systemctl start myunit.timer.

This post is licensed under CC BY 4.0 by the author.