In the previous post, the crontab was covered. Systemd gives another option, timers. This works basically the same way, it’s just a job scheduler.
Creating a timer
Creating a timer is simply creating a file. This will live in “~/.local/share/systemd/user”. Let’s name it example.timer.
[Unit] Description=ExampleTimer which runs every 10 minutes [Timer] OnCalendar=*:0/10 Unit=example.service
The first parts are pretty basic, the description and the program to run (ExecStart). The OnCalendar= part is where you setup the schedule. It has this format:
OnCalendar=[DayOfWeek] [Year-Month-Day] [Hour:Minute:Second]
- DayOfWeek – 3 character day of the week (i.e. Wed,Thu,Sun)
- Year-Month-Day – Standard calendar dates
- Hour:Minute:Second – Standard 24-hour time
Each value may contain a star to act as a wildcard and match any value. Each part is optional, but at least one piece is required.
Enabling the timer
You have to enable the timer via systemctl for it to be active:
systemctl --user enable example-timer
Here are some examples, with their crontab equivalent:
#Run every minute #Cron: * * * * * OnCalendar=*:* #Run at the top of every hour #Cron: 0 * * * * OnCalendar=*:0 #Run at noon every day #Cron: 0 12 * * * OnCalendar=12:0 #Run at 4:25am, every Monday #Cron: 25 4 * * 1 OnCalendar=Mon 4:25 #Run 2 hours before Christmas day #Cron: 00 22 24 12 * OnCalendar=*-12-24 22:00 #Runs Every Monday morning at midnight #Cron: 0 0 * * 1 OnCalendar=Mon 00:00