Originally this article was going to be published together with the first two articles in this series. However, I encountered an accident yesterday when I was searching for information and summarizing it, so I postponed it a bit.
The content of this article mainly refers to this blog post: (Baidu’s top search link for cron expressions). Try to write something a little different from others. Although, the content is mostly similar.
Let’s start with an example:
“0 0 10,14,16 * *?” What does it mean?
First of all, because the Cron expression is a string, the string is separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron has the following two syntax formats :
# Seconds MINUTES HOURS Dayofmonth Month Dayofweek Year or
Seconds MINUTES HOURS DAYOFMONTH MOY Dayofweek
"/": Indicates the starting time. Start triggering, and then trigger it every fixed time. For example, using 5/20 in the Minutes field means that it triggers once every 5 minutes, and triggers once every 25, 45, etc.
Seconds Minutes Hours DayofMonth Month DayofWeek Year or Seconds Minutes Hours DayofMonth Month DayofWeek
Seconds: Four characters ", - * /" can appear, and the valid range is 0 Integer of -59
Minutes: Four characters ", - * /" can appear, the valid range is 0-59 Integer
Hours: Four characters ", - * /" can appear, the valid range is 0 -23 integer
DayofMonth: eight characters ", - * / ? L W C" can appear, the valid range is an integer of 0-31
Month: four characters ", - * /" can appear, the valid range It is an integer from 1 to 12 or JAN-DEc
DayofWeek: The four characters ", - * / ? L C #" can appear, and the valid range is an integer from 1 to 7 or the two ranges of SUN-SAT. 1 means Sunday, 2 means Monday, and so on.
Year: Four characters ", - * /" can appear, and the valid range is 1970-2099.
(1)*: Indicates matching any value in this field. If * is used in the Minutes field, it means that the event will be triggered every minute.
(2)?: Can only be used in the 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 falls, you can only use the following writing method: 13 13 15 20 * ?, where the last digit can only be used? , but * cannot be used. If * is used, it means that it will be triggered regardless of the day of the week, which is not actually the case.
(3)-: Indicates the range. For example, using 5-20 in the Minutes field means that it triggers every minute from 5 minutes to 20 minutes.
(4)/: Indicates the start time. Trigger, and then trigger every fixed time. For example, using 5/20 in the Minutes field means that it triggers once every 5 minutes, and triggers once every 25, 45, etc.
(5),: means listing enum value value. For example: using 5,20 in the Minutes field means that it is triggered every minute at 5 and 20 minutes.
(6)L: means last and can only appear in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, it means it will be triggered on the last Thursday.
(7)W: Indicates valid working days (Monday to Friday), which can only appear in the DayofMonth field. The system will trigger the event on the nearest valid working day to the specified date. For example: using 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 a Sunday, it will be triggered on the 6th (Monday); if the 5th is on one of Monday to Friday, it will be triggered on the 5th. Another point is that the latest search for W will not span months
(8)LW: These two characters can be used together to indicate the last working day of a certain month, that is, the last Friday.
(9)#: Used to determine the day of the week of each month and can only appear in the DayofMonth field. For example, in 4#2, it means the second Wednesday of a certain month.
A few examples:
0 0 2 1 * ? * means scheduling tasks at 2 a.m. on the 1st of each month
0 15 10 ? * MON-FRI means Monday to Friday Execute the job every day at 10:15 am
0 15 10 ? 6L 2002-2006 means execute the job at 10:15 am on the last Friday of each month from 2002 to 2006
A cron expression There are at least 6 (possibly 7) space-separated time elements.
In order, they are
seconds (0~59)
minutes (0~59)
hours (0~23)
days (months) (0~31, but you need to consider The number of days in your month)
month (0~11)
day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)
year (1970- 2099)
Each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a List (1,3,5), wildcard. Since the two elements "date of month" and "date of week" are mutually exclusive, one of them must be set?
Some subexpressions can include Some 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, "*" in the sub-expression (month) represents the meaning of each month, and "*" in the sub-expression (day (week)) represents the day of the week Each day
The "/" character is used to specify the increment of the value
For example: "0/15" in the subexpression (minutes) means that starting from the 0th minute, every 15 minutes
The "3/20" in the subexpression (minutes) means that 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 sub-expressions of day (month) and day (week), indicating that no value is specified
When one of the two sub-expressions is specified with a value, in order to avoid conflicts, it is necessary to Set the value of another subexpression to "?"
The "L" character is only used in the day (month) and day (week) subexpressions, which is the abbreviation of the word "last"
But its meaning in the two subexpressions is different.
In the day (month) sub-expression, "L" represents the last day of the month
In the day (week) self-expression, "L" represents the last day of the week, which is SAT
If there is specific content before "L", it has other meanings
For example: "6L" means the sixth to last day of this month, "FRIL" means the last day of this month One Friday
Note: When using the "L" parameter, do not specify a list or range as this will cause problems
Special characters allowed for field allowed values
Seconds 0-59, - */
Minute 0-59, - * /
Hour 0-23, - * /
Date 1-31, - * ? / L W C
Month 1-12 or JAN-DEC, - * /
Weekday 1-7 or SUN-SAT, - * ? / L C
#Year (optional) Leave blank, 1970-2099, - * /
2. Cron expression example:
"0 0 12 * * ?" Triggered every day at 12 noon
"0 15 10 ? * *" Triggered every day at 10:15 am
"0 15 10 * * ?" Triggered every day at 10:15 am
"0 15 10 * * ? *" Triggered every day at 10:15 AM
"0 15 10 * * ? 2005" Triggered every day at 10:15 AM in 2005
"0 * 14 * * ?" Trigger every 1 minute from 2pm to 2:59pm every day
"0 0/5 14 * * ?" Trigger every 5 minutes from 2pm to 2:55pm every day
"0 0/5 14,18 * * ?" Triggers every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 6:55pm
"0 0-5 14 * * ?" Triggers every afternoon Triggered every 1 minute from 2:00 to 2:05 pm
"0 10,44 14 ? 3 WED" Triggered every year on Wednesdays in March at 2:10 pm and 2:44 pm
"0 15 10 ? * MON-FRI" Triggers at 10:15 am from Monday to Friday
"0 15 10 15 * ?" Triggers at 10:15 am on the 15th of each month
"0 15 10 L * ?" On the last day of each month Triggered at 10:15 AM
"0 15 10 ? * 6L" Triggered at 10:15 AM on the last Friday of each month
"0 15 10 ? * 6L 2002-2005" Every month from 2002 to 2005 Triggered at 10:15 AM on the last Friday
"0 15 10 ? * 6#3" Triggered at 10:15 AM on the third Friday of each month
The above is the detailed content of Related content of cron expression based on the simplest scheduled task implementation and configuration based on Spring. For more information, please follow other related articles on the PHP Chinese website!