Saturday 4 February 2023

Azure Functions – Time Trigger (CRON) Cheat Sheet

 This is a cheat sheet for CRON expressions that are used in the time triggers for Azure functions. They define how often a trigger/the Azure function should be executed (daily, hourly, every 3 months, …).

The basic format of the CRON expressions in Azure is:
{second} {minute} {hour} {day} {month} {day of the week}
e.g. 0 * * * * * (=every minute)

The following values are allowed for the different placeholders:

ValueAllowed ValuesDescription
{second}0-59; *{second} when the trigger will be fired
{minute}0-59; *{minute} when the trigger will be fired
{hour}0-23; *{hour} when the trigger will be fired
{day}1-31; *{day} when the trigger will be fired
{month}1-12; *{month} when the trigger will be fired
{day of the week}0-6; SUN-SAT; *{day of the week} when the trigger will be fired

e.g. 3 5 * * * * defines a trigger that runs every time when the clock is at second 3 and minute 5 (e.g. at 09:05:03, 10:05:03, 11:05:03, …).

The trigger executes at UTC timezone. So for Vienna (UTC+1), a trigger at 18:00 (UTC) executes at 19:00 Vienna time (UTC+1).

Examples

ExpressionDescriptionruns at
0 * * * * *every minute09:00:00; 09:01:00; 09:02:00; … 10:00:00
0 */5 * * * *every 5 minutes09:00:00; 09:05:00
0 0 * * * *every hour (hourly)09:00:00; 10:00:00; 11:00:00
0 0 */6 * * *every 6 hours06:00:00; 12:00:00; 18:00:00; 00:00:00
0 0 8-18 * * *every hour between 8-1808:00:00; 09:00:00; … 18:00:00; 08:00:00
0 0 0 * * *every day (daily)Mar 1, 2017 00:00:00; Mar 2, 2017 00:00:00
0 0 10 * * *every day at 10:00:00Mar 1, 2017 10:00:00; Mar 2, 2017 10:00:00
0 0 * * * 1-5every hour on workdaysMar 3 (FRI), 2017 22:00:00; Mar 3 (FRI), 2017 23:00:00; Mar 6 (MON), 2017 00:00:00
0 0 0 * * 0every sunday (weekly)Mar 5 (SUN), 2017 00:00:00; Mar 12 (SUN), 2017 00:00:00
0 0 9 * * MONevery monday at 09:00:00Mar 6 (MON), 2017 09:00:00; Mar 13 (MON), 2017 09:00:00
0 0 0 1 * *every 1st of month (monthly)Mar 1, 2017 00:00:00; Apr 1, 2017 00:00:00; May 1, 2017 00:00:00
0 0 0 1 1 *every 1st of january (yearly)Jan 1, 2017 00:00:00; Jan 1, 2018 00:00:00; Jan 1, 2019 00:00:00
0 0 * * * SUNevery hour on sundayMar 5 (SUN), 2017 23:00:00; Mar 12 (SUN), 2017 00:00:00; Mar 12 (SUN), 2017 01:00:00
0 0 0 * * SAT,SUNevery saturday and sundayMar 3 (SUN), 2017 00:00:00; Mar 11 (SAT) 00:00:00; Mar 12 (SUN), 2017 00:00:00
0 0 0 * * 6,0every saturday and sundayMar 3 (SUN), 2017 00:00:00; Mar 11 (SAT) 00:00:00; Mar 12 (SUN), 2017 00:00:00
0 0 0 1-7 * SUNevery first sunday of the month at 00:00:00Mar 5 (SUN), 2017 00:00:00; Apr 2 (SUN), 2017 00:00:00
11 5 23 * * *daily at 23:05:11Mar 1, 2017 23:05:11; Mar 2, 2017 23:05:11
30 5 /6 * * *every 6 hours at 5 minutes and 30 seconds06:05:30; 12:05:30; 18:05:30; 00:05:30
*/15 * * * * *every 15 seconds09:00:15; 09:00:30; … 09:03:30; 09:03:45; 09:04:00

(the most common expressions are bold)

Attention: the following CRON expression is valid and you can use it, but there is an issue with it:
0 0 */5 * * *
It means every 5 hours. 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. So it should be dividable by the maximum value. The following values are good if you want a regular frequency:

  • 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, because there are leap years and months with 28, 29, 30 or 31 days.

All other values can lead to “wrong” executions at the end of a “cycle”. e.g. every 7 hours executes at 00:00, 07:00, 14:00, 21:00, 00:00 (only 3 hours – not 7)

Additional information

Azure Functions timer trigger: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer
Wikipedia CRON expression: https://en.wikipedia.org/wiki/Cron#CRON_expression

No comments:

Post a Comment