Cron表達式是一個(gè)字符串,字符串以 5 or 6 個(gè)空格隔開(kāi),分為 6 or 7 個(gè)域,每一個(gè)域代表一個(gè)含義,Cron有如下兩種語(yǔ)法格式:
(1)?Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2) Seconds Minutes Hours DayofMonth Month DayofWeek
I. Structure
corn: sec min hour day of month month day of week year
Part 2: The meaning of each field
Field | Allowable values | Allowed special characters |
Second(s) | Integers from 0 to 59 | , - * / ? ?四個(gè)字符 |
分( Minutes ) | Integers from 0 to 59 | , - * / ? ?四個(gè)字符 |
小時(shí)( Hours ) | Integers from 0 to 23 | , - * / ? ?四個(gè)字符 |
日期( DayofMonth ) | Integers from 1 to 31 (but consider the number of days in the month). | ,- * ? / L W C ? ? 八個(gè)字符 |
月份( Month ) | Integers 1-12 or Jan-Dec | , - * / ? ?四個(gè)字符 |
星期( DayofWeek ) | 1-7 integers or SUN-SAT (1=SUN) | , - * ? / L C # ? ? 八個(gè)字符 |
年(可選,留空)( Year ) | 1970~2099 | , - * / ? ?四個(gè)字符 |
Cautionary Notes:
Each domain uses numbers, but can also include the following special characters, which have the following meanings:
* : Indicates a match for any value in the domain. 假如在Minutes域使用*, 即表示每分鐘都會(huì )觸發(fā)事件。
(2) ?: Can only be used in DayofMonth and DayofWeek fields. It also matches any value of the field, but actually doesn't. 會(huì )。因為DayofMonth和DayofWeek會(huì )相互影響。例如想在每月的20日觸發(fā)調度,不管20日到底是星期幾,則只能使用如下寫(xiě)法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期幾都會(huì )觸發(fā),實(shí)際上并不是這樣。
(3) - : Indicates a range. 例如在Minutes域使用5-20,表示從5分到20分鐘每分鐘觸發(fā)一次
(4)/: Indicates triggering starting from the specified time, then triggering at fixed intervals. 例如在Minutes域使用5/20,則意味著(zhù)5分鐘觸發(fā)一次,而25,45等分別觸發(fā)一次.
(5): Indicating the enumeration values. 例如:在Minutes域使用5,20,則意味著(zhù)在5和20分每分鐘觸發(fā)一次。
L: Represents the last; can only appear in the DayOfWeek and DayOfMonth fields. 如果在DayofWeek域使用5L,意味著(zhù)在最后的一個(gè)星期四觸發(fā)。
W: Indicates valid working days (Mon-Fri), appearing only in the DayofMonth field. The system will trigger events on the nearest valid working day to the specified date. 例如:在 DayofMonth使用5W,如果5日是星期六,則將在最近的工作日:星期五,即4日觸發(fā)。如果5日是星期天,則在6日(周一)觸發(fā);如果5日在星期一到星期五中的一天,則就在5日觸發(fā)。另外一點(diǎn),W的最近尋找不會(huì )跨過(guò)月份 。
(8) LW: These two characters can be used together to denote the last working day of a month, i.e., the last Friday.
(9)#: Used to determine the nth occurrence of a weekday in a month, it can only appear in the DayofWeek field. 例如在4#2,表示某月的第二個(gè)星期三。
Section 3: Examples of Common Expressions
(0) 0/20 * * * * ? 表示每20秒 調整任務(wù)
(1) 0 0 2 1 * ? 表示在每月的1日的凌晨2點(diǎn)調整任務(wù)
(2) 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15執行作業(yè)
(3) 0 15 10 ? 6L 2002-2006 表示2002-2006年的每個(gè)月的最后一個(gè)星期五上午10:15執行作
(4) 0 0 10,14,16 * * ? 每天上午10點(diǎn),下午2點(diǎn),4點(diǎn)
(5) 0 0/30 9-17 * * ? 朝九晚五工作時(shí)間內每半小時(shí)
(6) 0 0 12 ? * WED 表示每個(gè)星期三中午12點(diǎn)
(7) 0 0 12 * * ? 每天中午12點(diǎn)觸發(fā)
(8) 0 15 10 ? * * 每天上午10:15觸發(fā)
(9) 0 15 10 * * ? 每天上午10:15觸發(fā)
(10) 0 15 10 * * ? * 每天上午10:15觸發(fā)
(11) 0 15 10 * * ? 2005 2005年的每天上午10:15觸發(fā)
(12) 0 * 14 * * ? 在每天下午2點(diǎn)到下午2:59期間的每1分鐘觸發(fā)
(13) 0 0/5 14 * * ? 在每天下午2點(diǎn)到下午2:55期間的每5分鐘觸發(fā)
(14) 0 0/5 14,18 * * ? 在每天下午2點(diǎn)到2:55期間和下午6點(diǎn)到6:55期間的每5分鐘觸發(fā)
(15) 0 0-5 14 * * ? 在每天下午2點(diǎn)到下午2:05期間的每1分鐘觸發(fā)
(16) 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44觸發(fā)
(17) 0 15 10 ? * MON-FRI 周一至周五的上午10:15觸發(fā)
(18) 0 15 10 15 * ? 每月15日上午10:15觸發(fā)
(19) 0 15 10 L * ? 每月最后一日的上午10:15觸發(fā)
(20) 0 15 10 ? * 6L 每月的最后一個(gè)星期五上午10:15觸發(fā)
(21) 0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一個(gè)星期五上午10:15觸發(fā)
(22) 0 15 10 ? * 6#3 每月的第三個(gè)星期五上午10:15觸發(fā)
Note:
Some sub-expressions can include ranges or lists.
For example, the sub-expression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED, SAT".
The "*" character represents all possible values.
Therefore, "*" in the sub-expression (month) signifies every month, and "*" in the sub-expression (day [weekday]) indicates every day of the week.
“/”字符用來(lái)指定數值的增量
例如:在子表達式(分鐘)里的“0/15”表示從第0分鐘開(kāi)始,每15分鐘
在子表達式(分鐘)里的“3/20”表示從第3分鐘開(kāi)始,每20分鐘(它和“3,23,43”)的含義一樣
“?”字符僅被用于天(月)和天(星期)兩個(gè)子表達式,表示不指定值
當2個(gè)子表達式其中之一被指定了值以后,為了避免沖突,需要將另一個(gè)子表達式的值設為“?”
“L” 字符僅被用于天(月)和天(星期)兩個(gè)子表達式,它是單詞“l(fā)ast”的縮寫(xiě)
但是它在兩個(gè)子表達式里的含義是不同的。
在天(月)子表達式中,“L”表示一個(gè)月的最后一天
在天(星期)自表達式中,“L”表示一個(gè)星期的最后一天,也就是SAT
If there is specific content before the "L," it assumes a different meaning.
例如:“6L”表示這個(gè)月的倒數第6天,“FRIL”表示這個(gè)月的最一個(gè)星期五
注意:在使用“L”參數時(shí),不要指定列表或范圍,因為這會(huì )導致問(wèn)題
You recently used: