DEV Community 👩‍💻👨‍💻

Andreas Jakof
Andreas Jakof

Posted on

CronJob-Expressions (Automate Tasks in Azure)

This post is mostly for me to remember and to look it up, when I need it again, but I hope, you might find it usefull as well.

There are tasks, which are run repeatedly over and over again. One of the most common might be a cleanup job for a database.

I personally prefer events, when something happens, but sometimes you do not have an event, and that is, when you need to run tasks on a schedule.

In Azure you basically have two options, to execute those:

  1. Azure WebJobs
  2. Azure Functions

The former runs as part of a WebApp for as long as it takes. The second runs "serverless" for a maximum of 10 minutes.

Both, however, take a CronJob-Expression as their scheduling information. So here are some information about those.

A CronJob-Expression is a string of mostly numbers and some literals describing the execution time/interval of a cron job in the following format:

second minute hour day month day of the week

Each of those data entries will create a set of restrictions to the time of execution and the result will be the combination of all those sets.

The rules:

  1. There is no empty set. If you don't want it to run, just don't schedule it.
  2. , is used to list single data entries e.g. 1,5,7,35
  3. - is used to list an inclusive range e.g. 1-5 = 1,2,3,4,5
  4. * is used to set the value to "any".
  5. / sets an interval for the reoccurence. No lists or ranges for reoccureances.
  6. Literals MON, TUE, WED, THU, FRI, SAT, SUN can be used for the day of the week. No Range, when using literals. Use 0-6 instead, starting with SUN = 0.

Lets have a look some examples:

Expression Description
0 * * * * * Every minute
0 1/2 * * * * Every other minute
*/1 * * * * * Every second
0 0 * * * * Every hour
0 0 0 * * * Every day
0 30 11 * * * Every day at 11:30:00
0 0 5-10 * * * Every hour between 5 to 10
0 0 0 * * SAT Every saturday
0 0 0 * * 6 Every saturday
0 0 0 * * 1-5 Every workday (Monday to Friday)
0 0 0 * * SAT,SUN Every saturday and sunday
0 0 0 1 1 * Every year 1st january
0 0 0 1-7 * SAT Every first saturday of the month at 00:00:00

Lets have a closer look at some of these:
0 1/2 * * * *:
The seconds are 0 -> only at full minutes.
The first time to execute, the minute must be 1 and after that every other minute /2.
All other entries can have any value -> every hour at every day.
It runs every odd minute.

*/1 * * * * *:
This could have been written also * * * * * * and will be executed every second until it is stopped.

0 0 0 1-7 * SAT:
That one is bit more interesting.
It exutes only at midnigth, since the parts that describe the time of the day are all set to zero. Execution is also restricted to the days within the range 1 through 7 (the first week) of the month and to Saturdays. To rephrase that a bit, it will be executed at midnight of every Saturday within the first week of every month.

When scheduling in Azure keep in mind, that times are given in UTC.

Top comments (0)

Super Useful CSS Resources

>> Check out this classic DEV post <<