In this blog we are going to learn the format of systemd timers and their onCalendar field. You can find what systemd timers are and why they are a better alternative to cron over here
Systemd Timer OnCalendar Format vs Cronjob format
1* * * * *2minute hours day-of-the-month month day-of-the-week
So for example if we want a job to run every time it is midnight. We will set
minute to 00 and
hours to 00 and everything else as it is,
That will give us -
100 00 * * *2# every day midnight
Similarly if we want it to to be every weekday(range) midnight it will be
100 00 * * 1-52# every day midnight
And the last concept in this is repition what if we want a job to run every
n minutes. Then we use this format.
1*/n * * * *2# every n minutes
Similarly these repititions can be use for any of the 5 fields along with absolute time.
Systemd Timer OnCalendar Format
Now that you know the basics of corn format it will be easier for you to understand the OnCalendarfomat which gives you more granularity and control.
So the basic format of Oncalnedar event is this -
1* *-*-* *:*:*
It is divided into 3 parts -
*- To signify the day of the week eg:- Sat,Thu,Mon
*-*-*- To signify the calendar date. Which means it breaks down to -
2021-10-15is 15th of October
*-10-15means every year at 15th October
*-01-01means every new year.
*:*:*is to signify the time component of the calnedar event. So it is -
Note- Unlike cronjob we can skip a component of oncalendarevent if we don’t have any modifications for it.
Which means -
1Wed *-*-* 17:48:002#Can also be written as ->3Wed, 17:48
Now onCalendar also abides all the rules of the cron format. So we will go into each concept one by one.
Systmed Timers which run on a specified point in time
This is fairly simple just fill the below fields with you desired point in time.
1* *-*-* *:*:*2#Day Of the week Year-Month-Date Hour:Minute:Second
Examples Shown at the end of the blog in TL;DR
Systemd Timers Oncalendar Examples which run in a given frequency
So as we used
/ for frequency in cron job we do the same here.
So for example for every 2 day we say
1*/1 *-*-* *:*:*
Similarly we can set the intitial period also. So every days starting from Monday would be -
1Mon/1 *-*-* *:*:*
And we can set range for the frquency to run on, So everyday but only weekday would be
1Mon,Tue,Wed,Thu,Fri *-*-* *:*:*
So Systemd Timers Oncalendar range examples and normal examples are given in the table below. Use the calendar command of systemd-analyze to validate and normalize calendar time specifications for testing purposes. The tool also calculates when a specified calendar event would occur next.
TL; DR (If you want to learn you can browse down too)
|Every 2 minute|
|Every 5 minutes|
|Every 15 minutes|
|Every quarter hour|
|Every 30 minutes|
|Every half an hour|
|Every 60 minutes|
|Every 1 hour|
|Every 2 hour|
|Every 3 hour|
|Every other hour|
|Every 6 hour|
|Every 12 hour|
|Between certain hours|
|Once A day|
|Every Day at 1am|
|Every day at 2am|
|Every day at midnight|
|Every night at midnight|
|Every friday at midnight|
|monday to friday|
|Every 7 days|
|once a week|
|once a month|
|Every 6 months|