How to use CRON expressions
In the majority of cases it is enough to use standard scheduler modes, for example âDailyâ, âWeeklyâ, âMonthlyâ, etc. In some rare cases it is required to schedule more difficult task. For example, to run the agent every 5 minutes between 8:00 AM and 11:00 AM every Monday, Wednesday and Friday during September. In such cases we need to use CRON expressions. CRON expression consists of 7 subexpressions divided by spaces. Each subexpression is responsible for some definite part of the process of scheduling.
# | Field | Possible value |
---|---|---|
1 | Seconds | 0-59 seconds |
2 | Minutes | 0-59 minutes |
3 | Hours | 0-23 hour |
4 | Day-of-Month | 1-31 (depending on month) |
5 | Month | 0-11 or rows JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC |
6 | Day-of-Week | 1-7 or rows SUN, MON, TUE, WED, THU, FRI and SAT |
7 | Year | optional field (4 digit year) |
Example of expression: 0 0 8 ? * MON means that the task will be run every Monday at 8 AM
Each subexpression is presented as some range or list. For example, MON can be changed to "MON-FRI,SUN" The values can be divided by comma, for example, MON,TUE,WED,THU,FRI and equivalent expression with diapason MON-FRI
- Symbol '*' - wild-card to indicate all possible values. For example, * in the field "Month" means every month, * in the field Day-of-Week means every day of the week.
- Symbol '/' is used to indicate incremental component, for example 10/5 in the field Minutes means "every 5 minutes" starting from the 10th minute. To put it in other words it is equivalent to 10,15,20,25,30,35,40,45,50,55.
- Symbol '?' is used in the fields Day-of-Month, Day-of-Week and means "arbitary meaning" . It is used when the field value should be neglected.
- Symbol 'L' is used in the fields Day-of-Month, Day-of-Week. It is a short form of "Last" for the field Day-of-Month and it means the last day of the month. For example, the last dat of January is 31, the last day of February in the leap year is 29. In the field Day-of-Ween the symbol acquires some other meaning: "last xxx of the month" , for example 2L or MONL means "the last Monday of the month" . Also it is possible to indicate a shift, for example L-2 means "two days before the end of the month" .
- Symbol 'W' is used to indicate the nearest working days. For example, 10W in the field Day-of Month means "the nearest working days on the 10th of this month"
- Symbol '#' is used to indicate "the nth XXX weekday of the month" . For example, expression 2#3 or MON#3 in the field Day-of-Week means "third Monday of the month"
Examples of CRON expressions
0 0/5 * * * ?
fires the task every 5 minutes.
0 30 12/2 * * ?
fires the task at (12:30 14:30, 16:30, etc)
0 20 10-13 ? * MON,FRI
fires the task at 10:20, 11:20, 12:20, 13:20 every Monday and Friday.