Kumar Ashwin Hubert
Ashwin's Blog

Follow

Ashwin's Blog

Follow
Understanding CRON Expressions

Understanding CRON Expressions

Kumar Ashwin Hubert's photo
Kumar Ashwin Hubert
·Aug 28, 2018·

6 min read

Play this article

Many Azure resources like web jobs, Azure functions, logic apps use CRON expression when they need to work with Time Triggers. A CRON expression lets you define a time trigger with the help of six fields. All the possible time triggers let it be 'Once an hour' to '14 times a day' can be defined using a CRON expression.

Understanding the expression

As mentioned earlier, a CRON expression includes six fields:

{second} {minute} {hour} {day} {month} {day-of-week}

And each field can have one of the following values:

TypeExampleTriggered
Specific Value0 5 * * * *At every hour at that hour past 5 minutes
All Values (*)0 * 5 * * *At the 5th hour of day (05:mm:00), trigger it every minute of the hour
A range ( - )8-10 * * * * *At hh:mm:08,hh:mm:09,hh:mm:10 where hh:mm is every minute of every hour. 3 times a minute at 8,9,10th second
A set ( , )1,5,25 * * * * *At hh:mm:01, hh:mm:05, hh:mm:25 where hh:mm is every minute of every hour. 3 times a minute at 1, 5, 25th second
An Interval ( / )0 */5 * * * *At hh:05:00, hh:10:00, hh:15:00 and so on till hh:55:00, hh:00:00 where hh is every hour. 12 times an hour

CRON Examples

Some of the frequently used CRON expression samples

ExpressionTriggered
*/10 * * * * *Every 15 seconds
0 * * * * *Every minute
15 * * * * *Every minute at 15th Second of that minute
0 */10 * * * *Once every 10 minutes
0 0 * * * *Once every hour at 0th minute
0 0 */4 * * *Once every 4 hours
0 0 9-17 * * *Once every hour from 9AM to 5pm
0 0 0 * * *Every day at 00:00:00
0 30 10 * * *at 10:30 am every day
0 30 10 2 * *at 10:30 am every 2nd day of every month
0 30 10 * * 0,6at 10:30 am every weekend (Sat, Sun)
0 30 10 * * 1-5at 10:30 am every weekday
0 0 * * * WED,FRIEvery hour on Wednesday and Friday
0 0 0 * * SUNEvery Sunday at 00:00:00
0 0 0 1-7 * SUNEvery first Sunday of the month at 00:00:00
0 0 0 1 * *Every 1st of month at 00:00:00
0 0 0 1 1 *Every 1st of January every year
0 0 * * 1 *Every hour in January
30 5 */6 * * *Every 6 hours at 5 minute 30 seconds

Common pitfalls:

The CRON expression 0 0 */5 * * * means every 5 hours. So it executes at 00:00:00, 05:00:00, 10:00:00, 15:00:00, 20:00:00, 00:00:00 ... So it's not exactly every 5 hours. If you want a regular frequency, the following expressions are good:

  • For minutes and seconds : /2, /3, /4, /5, /6, /10, /12, /15, /20 and /30 (60 is divisible by these numbers)
  • For hours : /2, /3, /4, /6, /8 and /12
  • For Months : /2, /3, /4 and /6
  • For days : Nothing actually because there are leap years and months with 28, 29, 30 or 31 days.

If you want a cyclic approach, all other values can lead to "wrong" executions at the end of the cycle. e.g. Every 10 hours executes at 00:00:00, 10:00:00, 20:00:00, 00:00:00 (only 4 hours, not 10).

You can find CRON expression examples online, but many of them omit the {second} field. In case you copy one of them, remember to add the missing {second} field. Also, you will want a zero in that field and not an asterisk

Additional references:

Time trigger for Azure Functions: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

CRON Expression Descriptor: https://cronexpressiondescriptor.azurewebsites.net/

Did you find this article valuable?

Support Kumar Ashwin Hubert by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this