One, foreword
Recently, we need to implement a module to execute and manage scheduled tasks. When using Quartz package, we need to use Cron expression to define the execution strategy of scheduled tasks. Therefore, we need to sort out its usage.
Second, the definition of
First of all, the Cron expression is represented as a field consisting of seven fields (parts), with each module representing a date-time area. Its most basic form is:
[second] [minute] [hour] [day] [month] [week] [year]Copy the code
Three, break down
3.1 Definition of each module
The definitions of Cron fields are shown in the following table:
The domain | If required | Values and ranges | Wildcards are available |
---|---|---|---|
seconds | is | 0-59 | – * / |
points | is | 0-59 | , – * / |
when | is | 0-23 | , – * / |
day | is | 1-31 | , – *? / L W |
month | is | 1-12 or JAN – DEC | , – * / |
weeks | is | 1-7 – SAT or SUN | , – *? / L # |
years | no | 1970-2099. | , – * / |
3.2 Wildcard characters in Crontab
-
If we define 8,12,35 in the “points” field, it means that the scheduled task is executed at 8,12 and 35, respectively.
-
– Specifies the contiguous range in a field. If we define 1-6 in the field of “time”, it means that it is triggered every hour between 1 and 6. With, wildcard equals 1,2,3,4,5,6.
-
* represents all values, which can be read as “every”. If set to * in the day field, it will be triggered every day.
-
? Indicates that no value is specified. Use scenarios that do not care about the current value of this field. Primary user time represents when there is a conflict, such as day and week. For example, to trigger an action on the 8th of the month, but not the day of the week, we could set 0, 0, 0, 8 *? .
-
/ is emitted periodically on a field. This symbol divides the expression in its field into two parts. The first part is the starting value, which decreases by one unit except for seconds. On minute, the command is executed every 10 minutes starting from the fifth second.
-
L stands for “LAST”, which can only be used in “day” and “week”. It is set in “Day”, indicating the last day of the month (depending on the current month, or if it is February, depending on whether it is a calendar year), and in “week” indicating Saturday, which is equivalent to “7” or “SAT”. If the “L” is preceded by a number, it indicates the last of the data. For example, if the format of 7L is set on Week, it means the last Saturday of the month.
-
W indicates the latest business day (Monday to Friday) to the specified date. It can only be used in days and after a specific number. If 15W is set to Day, it is triggered on the working day nearest the 15th of each month. If the 15th falls on a Saturday, the next Friday (14th) will be triggered. If the 15th falls on a weekend, the next Monday (16th) will be triggered. If the 15th falls on a weekday (Monday to Friday), it is triggered on that day. If the value is 1W, it can only be pushed to the next working day of the month, not to the next month.
-
# indicates the day of the month. For example, “2#3” is on the third Tuesday of each month.
Fourth, the sample
Here are some examples to practice reading according to the above explanation:
- Run this command every 1 minute:
0 */1 * * *?
- Once a day at 22:00:
0, 0, 22 * *?
- Once at 1:00 a.m. on the first day of each month:
0, 0, 1, 1 star?
- Once at 23:00 on the last day of each month:
0 0 23 L * ?
- Once every Saturday at 3am:
0 0 3? * L
- Once at 24 and 30:
0 24,30 * * *?
Five, the summary
In addition, Amway has several online generation Cron sites:
Online Cron expression generator Quartz /Cron/Crontab expression generator