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
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 -year-month-date
.2021-10-15
is 15th of October*-10-15
means every year at 15th October*-01-01
means every new year.
*:*:*
is to signify the time component of the calnedar event. So it is -hour:minute:second
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.
1
TL; DR
OnCalendar Examples, Systemd Timer Examples
Explaination | Systemd timer |
Every Minute | *-*-* *:*:00 |
Every 2 minute | *-*-* *:*/2:00 |
Every 5 minutes | *-*-* *:*/5:00 |
Every 15 minutes | *-*-* *:*/15:00 |
Every quarter hour | *-*-* *:*/15:00 |
Every 30 minutes | *-*-* *:*/30:00 |
Every half an hour | *-*-* *:*/30:00 |
Every 60 minutes | *-*-* */1:00:00 |
Every 1 hour | *-*-* *:00:00 |
Every 2 hour | *-*-* */2:00:00 |
Every 3 hour | *-*-* */3:00:00 |
Every other hour | *-*-* */2:00:00 |
Every 6 hour | *-*-* */6:00:00 |
Every 12 hour | *-*-* */12:00:00 |
Hour Range | *-*-* 9-17:00:00 |
Between certain hours | *-*-* 9-17:00:00 |
Every day | *-*-* 00:00:00 |
Daily | *-*-* 00:00:00 |
Once A day | *-*-* 00:00:00 |
Every Night | *-*-* 01:00:00 |
Every Day at 1am | *-*-* 01:00:00 |
Every day at 2am | *-*-* 02:00:00 |
Every morning | *-*-* 07:00:00 |
Every midnight | *-*-* 00:00:00 |
Every day at midnight | *-*-* 00:00:00 |
Every night at midnight | *-*-* 00:00:00 |
Every sunday | Sun *-*-* 00:00:00 |
Every friday | Fri *-*-* 01:00:00 |
Every friday at midnight | Fri *-*-* 00:00:00 |
Every saturday | Sat *-*-* 00:00:00 |
Every weekday | Mon...Fri *-*-* 00:00:00 |
weekdays only | Mon...Fri *-*-* 00:00:00 |
monday to friday | Mon...Fri *-*-* 00:00:00 |
Every weekend | Sat,Sun *-*-* 00:00:00 |
weekends only | Sat,Sun *-*-* 00:00:00 |
Every 7 days | * *-*-* 00:00:00 |
Every week | Sun *-*-* 00:00:00 |
weekly | Sun *-*-* 00:00:00 |
once a week | Sun *-*-* 00:00:00 |
Every month | * *-*-01 00:00:00 |
monthly | * *-*-01 00:00:00 |
once a month | * *-*-01 00:00:00 |
Every quarter | * *-01,04,07,10-01 00:00:00 |
Every 6 months | * *-01,07-01 00:00:00 |
Every year | * *-01-01 00:00:00 |