/

Cron Expressions

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 integer 0-59
  • minute: may have , - * /, valid value is integer 0-59
  • hour: may have , - * /, valid value is integer 0-23
  • day-of-month: may have , - * / ? L W C, valid value is integer 0-31
  • month: may have , - * /, valid value is integer 1-12 or JAN-DEC
  • day-of-week: may have , - * / ? L C #, valid value is integer 1-7 or SUN-SAT. 1 represent sunday, 2 represent monday…
  • year: may have , - * /, valid value is integer 1970-2099

Specials Characters In Expression

  • * (all): Means matching any value in the field. If using * in the minute field, it means that the event will be triggered every minute.
  • ? (any): Can only be applied to day-of-month and day-of-week field. 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-20 in the minute field 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 the minute field means triggering at 5 minutes, and 25 minutes, 45 minutes.
  • / (increments): Means enumerated values. For example: using 5,20 in the minute field means triggering at 5 and 20 minutes.
  • L (last): Means at the end. It can only appear in the day-of-week and day-of-month fields. If 5L is used in the day-of-week field, 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-month field. The system will trigger an event on the closest valid working day from the specified date. For example: 5W is used in day-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-month field. For example, 4#2 means the second Wednesday of a month.

Examples

30 * * * * ?: Trigger task every 30 seconds
30 10 * * * ?: Trigger task at 10 minutes and 30 seconds per hour
30 10 1 * * ?: Trigger task every day at 1:10:30
30 10 1 20 * ?: Trigger task at 1:10:30 on the 20th of each month
30 10 1 20 10 ? *: Trigger task at 1:10:30 on the October 20th of each year
30 10 1 20 10 ? 2011: Trigger task at 1:10:30 on the October 20th of 2011
30 10 1 ? 10 * 2011: Trigger task every day at 1:10:30 in October 2011
30 10 1 ? 10 SUN 2011: Trigger task every Sunday at 1:10:30 in October 2011
15,30,45 * * * * ?: Trigger task every 15 seconds, 30 seconds, and 45 seconds
15-45 * * * * ?: Trigger task every second from 15 to 45 seconds
15/5 * * * * ?: Trigger task every 5 seconds starting at 15 seconds
15-30/5 * * * * ?: Trigger task every 5 seconds between 15 and 30 seconds every minute
0 0/3 * * * ?: Trigger task every 3 minutes starting 0 minutes and 0 seconds
0 15 10 ? * MON-FRI: Trigger task from 10:15:00 from Monday to Friday
0 15 10 L * ?: Trigger task at 10:15:00 on the last day of each month
0 15 10 LW * ?: Trigger task at 10:15:00 on the last working day of each month
0 15 10 ? * 5L: Trigger task at 10:15:00 on the last Thursday of every month
0 15 10 ? * 5#3: Trigger task at 10:15:00 on the third week of each month