A Cron expression is a string separated by five or six Spaces and divided into six or seven fields, each field representing a meaning. Cron has the following two syntax formats:

(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year

(2) Seconds Minutes Hours DayofMonth Month DayofWeek

  

A, structure,

Corn from left to right (separated by Spaces) : second, minute, hour, date in a month, month, date in a week, year

Two, the meaning of each field

field permitted Special characters allowed
Seconds (Seconds) An integer 0 ~ 59 , – * / Four characters
(Minutes) An integer 0 ~ 59 , – * / Four characters
Hours An integer 0 ~ 23 , – * / Four characters
Date (DayofMonth) Integers from 1 to 31 (but you need to consider the number of days in your month) , – *? / L W C Eight characters
Month The value is an integer ranging from 1 to 12 or jan-dec , – * / Four characters
DayofWeek An integer from 1 to 7 or Sun-sat (1=SUN) , – *? / L C # eight characters
Year (optional, left blank) (Year) 1970 ~ 2099 , – * / Four characters

 

 

 

 

 

 

 

Matters needing attention:

Each field uses numbers, but the following special characters can also appear, which mean:

(1) * : matches any value of the field. If you use * in the Minutes field, it means that events are emitted every minute.

(2)? : Applies only to the DayofMonth and DayofWeek domains. It also matches any value of the field, but it doesn’t. Because DayofMonth and DayofWeek affect each other. For example, if you want to trigger scheduling on the 20th of each month, you can only use the following command: 13, 13, 15, 20 *? , the last digit can only be used? If * is used to indicate that the trigger is triggered no matter what day of the week, which is not the case.

(3) – : indicates the range. For example, using 5-20 in the Minutes field triggers every minute from 5 to 20 Minutes

(4) / : indicates that the trigger starts at the start time and then is triggered at fixed intervals. For example, using 5/20 in the Minutes field means that it fires once every 5 Minutes, while 25,45, and so on fire once.

(5), : lists enumeration values. For example, using 5,20 in the Minutes field means firing at 5 and 20 Minutes per minute.

(6) L: indicates that it can only be displayed in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, it is triggered on the last Thursday.

(7) W: indicates a valid working day (Monday to Friday), which can only be displayed in the DayofMonth field. The system triggers an event on the latest valid working day to the specified day. For example, if 5W is used in DayofMonth, if the 5th falls on a Saturday, it will be triggered on the nearest working day: Friday, the 4th. If the 5th falls on a Sunday, the 6th (Monday) is triggered; If the 5th falls on a day between Monday and Friday, it is triggered on the 5th. Another point is that the recent search for W does not cross months.

(8) LW: These two characters can be used together to indicate the last working day of a month, i.e. the last Friday.

(9) #: used to determine the day of the month, only in the DayofMonth field. For example, 4#2 is the second Wednesday of a month.

Examples of common expressions

(1) 0 0 2 1 *? * Indicates that tasks are adjusted at 2am on the first day of each month

(2) 0 15 10? * Mon-FRI indicates that the job will be executed at 10:15 a.m. each day, Monday through Friday

(3) 0 15 10? 6L 2002-2006 indicates the last Friday of each month in 2002-2006 at 10:15 am

(4) 0 0 10,14,16 * *? Every day at 10 a.m., 2 p.m., 4 p.m

(5) 0/30 9-17 * *? Every half hour during your 9-5 working hours

(6) 0 0 12? * WED means every Wednesday at 12 noon

(7) 0 0 12 * *? It’s triggered every day at 12 noon

(8) 0, 15, 10? * * triggers every day at 10:15 a.m

(9) 0 15 10 * *? Triggered every day at 10:15 a.m

(10) 0 15 10 * *? * Triggered every day at 10:15 a.m

(11) 0 15 10 * *? 2005 2005 triggered every day at 10:15 a.m

(12) 0 * 14 * *? Triggered every minute between 2 p.m. and 2:59 p.m. each day

(13) 0 0/5 14 * *? Triggered every 5 minutes between 2 p.m. and 2:55 p.m. each day

(14) 0/5 14,18 * *? Triggered every 5 minutes between 2 p.m. and 2:55 p.m. and 6 p.m. and 6:55 p.m. each day

(15) 0 0-5 14 * *? Triggered every minute between 2 p.m. and 2:05 p.m. each day

(16) 0, 10,44, 14? 3 WED triggers at 2:10 PM and 2:44 PM on Wednesdays of March each year

(17) 0, 15, 10? * Mon-FRI triggers Monday through Friday at 10:15 a.m

(18) 0, 15, 10, 15 times? Triggered at 10:15 am on the 15th of each month

(19) 0 15 10 L *? Triggered at 10:15 a.m. on the last day of each month

(20) 0, 15, 10? * 6L triggers at 10:15 am on the last Friday of every month

(21) 0, 15, 10? * 6L 2002-2005 The last Friday of each month from 2002 to 2005 was triggered at 10:15 am

(22) 0, 15, 10? * 6#3 triggers at 10:15 am on the third Friday of each month

 

  

Note:

(1) Some subexpressions can contain ranges or lists

For example, the subexpression (day (week)) can be “mon-fri”, “MON, WED, FRI”, “mon-wed,SAT”

The * character represents all possible values

Therefore, “*” represents the meaning of each month in the subexpression (month), and “*” represents each day of the week in the subexpression (day (week))

The “/” character is used to specify the increment of a value. For example, “0/15” in the subexpression (minutes) means the beginning of the 0 minute, and “3/20” in the subexpression (minutes) means the beginning of the 3 minute, and every 20 minutes (it has the same meaning as “3, 23, 43”)

“?” The day (month) and day (week) subexpressions are used only, indicating that no value is specified. When one of the two subexpressions is specified, the value of the other subexpression is set to? To avoid conflicts.

The “L” character is used only in day (month) and day (week) subexpressions. It is a contraction of the word “last” but has a different meaning in the two subexpressions. In the day (month) subexpression, “L” indicates the last day of a month. In the day (week) self-expression, “L” indicates the last day of a week, that is, SAT

If there is something concrete before the “L”, it has other meanings

For example: “6L” means the sixth to last day of the month, “FRIL” means the last Friday of the month Note: Do not specify lists or ranges when using the “L” argument, as this can cause problems