Cron expression is a string comprising five or six fields separated by white space that represents a set of times. It is widely used to execute some scheduled tasks. It can be used with Spring Scheduling Tasks, or linux scheduled tasks.
Expression
<second> <minute> <hour> <day-of-month> <month> <day-of-week> <year>
where <year> field is optional.
- second: may have
, - * /, valid value is integer0-59 - minute: may have
, - * /, valid value is integer0-59 - hour: may have
, - * /, valid value is integer0-23 - day-of-month: may have
, - * / ? L W C, valid value is integer0-31 - month: may have
, - * /, valid value is integer1-12orJAN-DEC - day-of-week: may have
, - * / ? L C #, valid value is integer1-7orSUN-SAT.1represent sunday,2represent monday… - year: may have
, - * /, valid value is integer1970-2099
Specials Characters In Expression
- * (all): Means matching any value in the field. If using
*in theminutefield, it means that the event will be triggered every minute. - ? (any): Can only be applied to
day-of-monthandday-of-weekfield. It also matches any value of the field. For example, if you want to trigger the scheduling on the 20th of each month, no matter what day of week the 20th is, you can only use the following expression:13 13 15 20 * ?. The last bit can only use?. - – (range): Means range. For example, Using
5-20in theminutefield means that it is triggered every minute from 5 minutes to 20 minutes. - , (values): Means that the trigger starts at the start time, and then triggers at regular intervals. Write as
start/intervals. For example, using 5/20 in theminutefield means triggering at 5 minutes, and 25 minutes, 45 minutes. - / (increments): Means enumerated values. For example: using
5,20in theminutefield means triggering at 5 and 20 minutes. - L (last): Means at the end. It can only appear in the
day-of-weekandday-of-monthfields. If5Lis used in theday-of-weekfield, it means that it is triggered on the last Thursday of the month. - W (weekday): Means a valid working day (Monday to Friday). It can only appear in the
day-of-monthfield. The system will trigger an event on the closest valid working day from the specified date. For example:5Wis used inday-of-month. If the 5th is Saturday, it will be triggered on the nearest 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 of Monday to Friday, it will be triggered on the 5th. In addition, W’s nearest search will not cross the month - LW: These two characters can be used together to indicate the last working day of a month, that is, the last Friday.
- #: It is used to determine the day of the week of each month and can only appear in the
day-of-monthfield. For example,4#2means the second Wednesday of a month.
Examples
30 * * * * ?: Trigger task every 30 seconds30 10 * * * ?: Trigger task at 10 minutes and 30 seconds per hour30 10 1 * * ?: Trigger task every day at 1:10:3030 10 1 20 * ?: Trigger task at 1:10:30 on the 20th of each month30 10 1 20 10 ? *: Trigger task at 1:10:30 on the October 20th of each year30 10 1 20 10 ? 2011: Trigger task at 1:10:30 on the October 20th of 201130 10 1 ? 10 * 2011: Trigger task every day at 1:10:30 in October 201130 10 1 ? 10 SUN 2011: Trigger task every Sunday at 1:10:30 in October 201115,30,45 * * * * ?: Trigger task every 15 seconds, 30 seconds, and 45 seconds15-45 * * * * ?: Trigger task every second from 15 to 45 seconds15/5 * * * * ?: Trigger task every 5 seconds starting at 15 seconds15-30/5 * * * * ?: Trigger task every 5 seconds between 15 and 30 seconds every minute0 0/3 * * * ?: Trigger task every 3 minutes starting 0 minutes and 0 seconds0 15 10 ? * MON-FRI: Trigger task from 10:15:00 from Monday to Friday0 15 10 L * ?: Trigger task at 10:15:00 on the last day of each month0 15 10 LW * ?: Trigger task at 10:15:00 on the last working day of each month0 15 10 ? * 5L: Trigger task at 10:15:00 on the last Thursday of every month0 15 10 ? * 5#3: Trigger task at 10:15:00 on the third week of each month