Detailed Explanation of cron Expressions in Timing Tasks

1. Structure

A cron expression is a string separated by 5 or 6 spaces and divided into 6 or 7 domains. Each domain 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 Minute Hour Month Date Month Date Week Year

2. The meaning of each field

field

allowance

special characters allowed

Seconds

Integer of 0~59

, - * / four characters

Minutes

Integer of 0~59

, - * / four characters

hours

Integer from 0 to 23

, - * / four characters

Date (Day of Month)

An integer from 1 to 31 (but you need to consider the number of days in your month)

,- * ? / LWC eight characters

month

Integer from 1 to 12 or JAN-DEC

, - * / four characters

Day of Week

An integer from 1 to 7 or SUN-SAT (1=SUN)

, - * ? / LC # eight characters

Year (optional, leave blank) (Year)

1970~2099

, - * / four characters

Each field uses numbers, but the following special characters can also appear, and their meanings are:

serial number symbol meaning
1 * means match any value of the field. If * is used in the Minutes field, it means that the event will be triggered every minute
2 ? It can only be used in DayofMonth and DayofWeek domains. It also matches any value of the domain, but it doesn't. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger scheduling on the 20th of each month, no matter what day of the week the 20th is, you can only use the following: 13 13 15 20 * ?, where the last digit can only be used? , instead of using *, if you use * to indicate that it will be triggered regardless of the day of the week, it is not the case
3 - Indicates the range. For example, use 5-20 in the Minutes field, indicating that it is triggered every minute from 5 minutes to 20 minutes
4 / Indicates that the trigger starts at the starting time, and then triggers every fixed time. For example, using 5/20 in the Minutes field means that it will trigger once every 5 minutes, while 25, 45, etc. will trigger once respectively.
5 , Indicates to list enumeration values. For example: using 5,20 in the Minutes field means that it will be triggered every minute at 5 and 20 minutes
6 L Indicates that at the end, it can only appear in the fields of DayofWeek and DayofMonth. If 5L is used in the DayofWeek field, it means that 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 field, and the system will trigger the event on the nearest valid working day to the specified date. For example: use 5W on DayofMonth, if the 5th is a Saturday, it will be triggered on the nearest working day: Friday, which is the 4th. If the 5th is Sunday, it will be triggered on the 6th (Monday); if the 5th falls on a day from Monday to Friday, it will be triggered on the 5th. Another point, W's most recent lookup does not span months
8 LW These two characters can be used together to indicate the last working day of a month, that is, the last Friday
9 # It is used to determine the day of the week of a certain month and can only appear in the DayofWeek field. For example, in 4#2, it means the second Wednesday of a certain month

3. Examples of common expressions

(1) 0 0 2 1 * ? * means to adjust the task at 2 am on the 1st of each month

(2) 0 15 10 ? * MON-FRI means that the job is executed at 10:15 every morning from Monday to Friday

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

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

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

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

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

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

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

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

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

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

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

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

(15) 0 0-5 14 * * ? Trigger every 1 minute from 2pm to 2:05pm every day

(16) 0 10,44 14 ? 3 WED Fired at 2:10 and 2:44 pm on Wednesdays every March

(17) 0 15 10 ? * MON-FRI Triggered at 10:15 AM Monday through 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 every month

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

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

Note:

Some subexpressions can contain ranges or lists

For example: subexpression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED, SAT" "*" 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 the value

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

"3/20" in the subexpression (minute) means starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")

The "?" character is only used in the two subexpressions of day (month) and day (week), indicating that no value is specified

When one of the two subexpressions is assigned a value, in order to avoid conflicts, it is necessary to set the value of the other subexpression to "?"

The "L" character is only used in the day (month) and day (week) subexpressions, which is an abbreviation for the word "last"

But its meaning in the two subexpressions is different.

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

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

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

For example: "6L" means the last 6th day of this month, "FRIL" means the last Friday of this month

Note: When using the "L" parameter, do not specify a list or range as this will cause problems

Guess you like

Origin blog.csdn.net/m0_48983233/article/details/122656494