web123456

Detailed explanation of cron expressions in timing tasks

1. Structure

A Cron expression is a string, separated by 5 or 6 spaces, divided into 6 or 7 fields, each field represents a meaning, Cron has the following two syntax formats:

(1)Seconds Minutes Hours DayofMonth Month DayofWeek Year

(2)Seconds Minutes Hours DayofMonth Month DayofWeek

Corn from left to right (separated by spaces):

Second point Hour Date in month month Date of the week years

2. Meaning of each field

Fields

Allowed values

Special characters allowed

Seconds

Integers of 0~59

, - * /    Four characters

Minutes

Integers of 0~59

, - * /    Four characters

Hours

Integers from 0 to 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

Integers of 1~12 or JAN-DEC

, - * /    Four characters

DayofWeek

Integers of 1~7 or SUN-SAT (1=SUN)

, - * ? / L C #     Eight characters

Year (optional, leave blank) (Year)

1970~2099

, - * /    Four characters

Each field uses numbers, but special characters can also appear as follows, and their meaning is:

Serial number symbol meaning
1 * Indicates any value matching the field. If you use * in the Minutes domain, it means that events will be triggered every minute.
2 ? It can only be used in the two domains of DayofMonth and DayofWeek. It also matches any value of the domain, but it does not. Because DayofMonth and DayofWeek will influence each other. For example, if you want to trigger the dispatch on the 20th of each month, no matter what day of the week it is, you can only use the following writing method: 13 13 15 20 * ?, Can the last one be used? , and cannot use *. If you use *, it will trigger no matter what day of the week, it is not actually the case
3 - Indicates range. For example, using 5-20 in the Minutes domain means that it triggers every minute from 5 minutes to 20 minutes
4 / Indicates that the start time starts to trigger, and then triggers every fixed time. For example, using 5/20 in the Minutes domain means that it triggers once every 5 minutes, while 25, 45, etc. triggers once every 25, 45, etc.
5 , Indicates listing enum values. For example: using 5,20 in the Minutes domain means triggering once per minute at 5 and 20 minutes
6 L Indicates that it can only appear in the DayofWeek and DayofMonth domains. If you use 5L in the DayofWeek domain, it means it will be triggered on the last Thursday
7 W Indicates a valid working day (Monday to Friday), which can only appear in the DayofMonth domain, and the system will trigger the event on the closest valid working day to the specified date. For example: Use 5W in DayofMonth, if the 5th is Saturday, it will be triggered on the most recent working day: Friday, that is, the 4th. If the 5th is Sunday, it will be triggered on the 6th (Monday); if the 5th is one day from Monday to Friday, it will be triggered on the 5th. Another point is that W's recent search will not cross the month
8 LW These two characters can be used in conjunction to indicate the last working day of a certain month, that is, the last Friday
9 # Used to determine the week of a certain month, it can only appear in the DayofWeek domain. For example, in 4#2, it means the second Wednesday of a certain month

3. Common expression examples

(1)0 0 2 1 * ? *   Indicates that the task is adjusted at 2 a.m. on the 1st of each month

(2) 0 15 10 ? * MON-FRI   Indicates that the homework is performed every day from Monday to Friday at 10:15 am

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

(4)0 0 10,14,16 * * ?   Every day at 10 am, 2 pm, 4 pm

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

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

(7)0 0 12 * * ?   Triggered at 12 noon every day

(8) 0 15 10 ? * *   Triggered at 10:15 am every day

(9)0 15 10 * * ?    Triggered every day at 10:15 am

(10)0 15 10 * * ? *   Triggered every day at 10:15 am

(11)0 15 10 * * ? 2005     2005 triggered at 10:15 am every day in 2005

(12) 0 * 14 * * ?    Triggered every 1 minute between 2 pm and 2:59 pm every day

(13)0 0/5 14 * * ?   Triggered every 5 minutes between 2 pm and 2:55 pm every day

(14)0 0/5 14,18 * * ?    Triggered every 5 minutes between 2:55 pm and between 6:55 pm and between 6:55 pm and every 5 minutes between 5:55 pm

(15)0 0-5 14 * * ?   Triggered every 1 minute between 2 pm and 2:05 pm every day

(16)0 10,44 14 ? 3 WED    Triggered at 2:10 and 2:44 pm on Wednesdays in March each year

(17)0 15 10 ? * MON-FRI     Triggered from 10:15 am from Monday to Friday

(18)0 15 10 15 * ?   Triggered at 10:15 am on the 15th of every month

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

(20)0 15 10 ? * 6L    Triggered at 10:15 am on the last Friday of each month

(21)0 15 10 ? * 6L 2002-2005   Triggered at 10:15 am on the last Friday of each month from 2002 to 2005 at 10:15 am on the 2002-2005 month

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

Note:

Some subexpressions can contain some range or list

For example: subexpression (day (week)) can represent all possible values ​​as "MON-FRI", "MON, WED, FRI", "MON-WED, SAT" and "*" characters

Therefore, "*" means the meaning of each month in the subexpression (month), and "*" means every day of the week in the subexpression (day (week))

The character "/" is used to specify the increment of the value

For example: "0/15" in the subexpression (minute) means starting from the 0th minute, every 15 minutes

"3/20" in the subexpression (minutes) means that every 20 minutes (it means "3, 23, 43") starts from the third minute.

"?" character is only used in two subexpressions: day (moon) and day (week), indicating that no value is specified

When one of the two subexpressions has a value specified, in order to avoid conflict, the value of the other subexpression needs to be set to "?"

The "L" character is only used for the two subexpressions of the sky (moon) and the sky (week), which is the abbreviation of the word "last".

But its meaning is different in the two subexpressions.

In the day (moon) expression, "L" means the last day of a month

In the day (week) self-expression, "L" means the last day of a week, which is SAT

If there is specific content before "L", it has other meanings

For example: "6L" means the sixth day to the end of the month, and "FRIL" means the most Friday of the month

Note: Do not specify a list or range when using the "L" parameter as this can cause problems