Use systemd timers as a replacement for cron: schedule services the same way you created them.

Create the timer

  • You need to create a .timer file in /etc/systemd/system/ with the same name as the service you want to schedule.
  • This is the basic content of a .timer file:
    [Unit]
    Description=timer description
    [Timer]
    OnBootSec=15min
    [Install]
    WantedBy=timers.target
    

[Unit]

  • Description: timer description.

[Timer]

  • OnBootSec: Defines a timer relative to when the computer was booted up. If you only use a number, means “seconds”. These are some examples of time formats:
    • 50
    • 30s
    • 15min
    • 5h 30min
    • 1d
    • 1w
  • OnActiveSec: Defines a timer relative to the moment the timer unit is activated.
  • OnCalendar: This option use system clock and not an event to define when the service should start. Uses the following format:
    DayOfWeek Year-Month-Day Hour:Minute:Second
    
    # Run every day at 5 p.m.
    OnCalendar=*-*-* 17:00:00
    # Run first day of the month only if that day is Monday
    OnCalendar=Mon *-*-01 12:00:00
    
    # You can use shortcuts like:
    OnCalendar=hourly
    OnCalendar=weekly
    
  • Unit: You can define the unit you want to control if it has a different name than the timer.

[Install]

  • WantedBy: intructions for the timer installation (systemctl enable). Typing timers.target would be fine for most cases. Services don’t need an [Install] section if they have a timer.

Start / Enable the timer

  • Reload systemd with sudo systemctl daemon-reload.
  • Like any other unit, you can type sudo systemctl start myservice.timer (replacing myservice with your timer name) to start the timer and sudo systemctl enable myservice.timer to enable the timer.

If you have any suggestion, feel free to contact me via social media or email.