[Cron expression] you don't have to worry about automatically executing tasks regularly and setting the time

 
May you be like sunshine, bright and not sad.

 

1. Cron expression

Kelon The unit of time, a krone, is one million years. Cron expression is a string separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron expression has the following two syntax formats:

  • Seconds Minutes Hours DayofMonth Month DayofWeek Year
  • Seconds Minutes Hours DayofMonth Month DayofWeek

 

2. Cron domain

  • Cron field value
Symbolexplain
*Indicates any value matching this field. If * is used in the Minutes field, it means that an event will be triggered every minute.
?It can only be used in DayofMonth and DayofWeek domains. It also matches any value of the field, but it won't, because DayofMonth and DayofWeek affect each other 1.
-Indicates the range. For example, using 5-20 in the Minutes field indicates that it is triggered every minute from 5 Minutes to 20 Minutes
/Indicates that the trigger starts at the start time, and then is triggered every fixed time. For example, if 5 / 20 is used in the Minutes field, it means that it is triggered every 5 Minutes, and 25, 45, etc. are triggered respectively
,Indicates that enumeration values are listed. For example, if 5 and 20 are used in the Minutes field, it means that they are triggered once in the 5th and 20th Minutes respectively
LIndicates that it can only appear in the DayofWeek and DayofMonth domains. If 5L is used in the DayofWeek domain, it means it will be triggered on the last Thursday
WIndicates that the valid working day (Monday to Friday) can only appear in the DayofMonth field. The system will trigger the event on the latest valid working day from the specified date 2 . On the other hand, W's recent search will not cross the month
LWThese two characters can be used together to represent the last working day of a month, that is, the last non Saturday weekend
CThis character depends on a specified calendar and can only appear in the DayofWeek and DayofMonth fields 3
#It is used to determine the day of the week of each month. It can only appear in the DayofWeek field. For example, in 4#2, it means the second Wednesday of a month
  • Characters that can appear in each field in Cron
     
    Seconds: can be an integer with four characters, - * / and the valid range is 0-59
    Minutes: can be an integer with four characters, - * / and the valid range is 0-59
    Hours: can be an integer with four characters, - * / and the valid range is 0-23
    DayofMonth: can appear, -* /? LWC is an eight character integer with a valid range of 0-31
    Month: can appear, -* / four characters, valid range is 1-12 integer or Jan Dec
    DayofWeek: can appear, -* /? LC# eight characters, valid range is 1-7 integer or SUN-SAT two ranges. 1 means Sunday, 2 means Monday, and so on
    Year: can appear, -* / four characters. The valid range is 1970-2099

 

3. Common examples of cron expressions

"*/5 * * * * ?"				Every 5 seconds:
"0 */1 * * * ?"				Every 1 minute:
"0 0 23 * * ?"				At 23:00 every day:
"0 0 1 * * ?"				Once a day at 1 a.m.:
"0 0 1 1 * ?"				At 1:00 a.m. on the 1st of each month:
"0 0 23 L * ?"				At 23:00 on the last day of each month:
"0 0 1 ? * L"				Once a week at 1 a.m. on Sunday:
"0 26,29,33 * * * ?"  		At 26 points, 29 points and 33 points:
"0 0 0,13,18,21 * * ?"		At 0:00, 13:00, 18:00 and 21:00 every day:
"0 0 2 1 * ? *" 			Indicates that the task is scheduled at 2 a.m. on the first day of each month
"0 15 10 ? * MON-FRI" 		It means to perform the work at 10:15 a.m. every day from Monday to Friday
"0 15 10 ? * 6L 2002-2006" 	Indicates 2002-2006 10 a.m. on the last Friday of each month in:15 Executive work
"0 0 10,14,16 * * ?" 		Every day at 10 a.m., 2 p.m. and 4 p.m
"0 0/30 9-17 * * ?" 		Every half an hour during nine to five working hours
"0 0 12 ? * WED" 			It means 12 noon every Wednesday
"0 0 12 * * ?" 				Triggered at 12 noon every day
"0 15 10 ? * *" 			10 a.m. every day:15 trigger
"0 15 10 * * ?" 			10 a.m. every day:15 trigger
"0 15 10 * * ? *" 			10 a.m. every day:15 trigger
"0 15 10 * * ? 2005" 		2005 10 a.m. every day in:15 trigger
"0 * 14 * * ?" 				Every day from 2 p.m. to 2 p.m:59 Triggered every 1 minute during
"0 0/5 14 * * ?" 			Every day from 2 p.m. to 2 p.m:55 Triggered every 5 minutes during
"0 0/5 14,18 * * ?" 		Every day from 2 p.m. to 2 p.m:55 During and from 6 p.m. to 6 p.m:55 Triggered every 5 minutes during
"0 0-5 14 * * ?" 			Every day from 2 p.m. to 2 p.m:05 Triggered every 1 minute during
"0 10,44 14 ? 3 WED"		Every Wednesday in March at 2 p.m:10 And 2:44 trigger
"0 15 10 ? * MON-FRI" 		Monday to Friday at 10 a.m:15 trigger
"0 15 10 15 * ?" 			10 a.m. on the 15th of each month:15 trigger
"0 15 10 L * ?" 			10 a.m. on the last day of each month:15 trigger
"0 15 10 ? * 6L" 			10 a.m. on the last Friday of each month:15 trigger
"0 15 10 ? * 6L 2002-2005" 	2002 10 a.m. on the last Friday of each month from 2005 to 2005:15 trigger
"0 15 10 ? * 6#3"  			 Triggered at 10:15 a.m. on the third Friday of each month
  1. 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 expression: 13 13 15 20 *?, The last one can only use?, You can't use *. If you use *, it will trigger regardless of the day of the week. In fact, it's not the case. ↩︎

  2. For example, 5W is used in DayofMonth. If the 5th is Saturday, it will be triggered on the latest working day: Friday, i.e. 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.
    The display will trigger no matter what day of the week. In fact, it is not. ↩︎

  3. That is, the value of this expression depends on the calculation results of the related calendar. If there is no calendar Association, it is equivalent to all contained calendars. For example, if the date field is "5C", it means the first day in the associated "calendar" or the next 5 days of the first day of the month. The week field "1C" indicates the first day in the associated calendar, or the day after the first day of the week, that is, the day after Sunday (Monday). ↩︎

Keywords: Linux Operation & Maintenance server cron

Added by madcat on Wed, 12 Jan 2022 04:00:28 +0200