定时任务Cron表达式详解
2018 - 12 - 10
Posted by paraller
表达式规则
格式

一个crond表达示由七个字段组成,可以在符合规则的条件下自由组合
| Field Name | Mandatory | Allowed Values | Allowed Special Characters |
|---|---|---|---|
| Seconds | YES | 0-59 | , - * / |
| Minutes | YES | 0-59 | , - * / |
| Hours | YES | 0-23 | , - * / |
| Day of month | YES | 1-31 | , - * ? / L W |
| Month | YES | 1-12 or JAN-DEC | , - * / |
| Day of week | YES | 1-7 or SUN-SAT | , - * ? / L # |
| Year | NO | empty, 1970-2099 | , - * / |
特殊字符:
-
*代表 每 秒/分钟/时 … -
?不指定具体的值
比如向指定 5月的第2天,但是不想指定哪一个星期的 ,就可以这样写0 0 0 2 5 ? -
-指定一个范围
比如10-12在 hour 这个字段 代表10、11、12点. -
,代表附加值
例如,MON,WED,FRI在 day-of-week 字段代表 “ Monday, Wednesday, and Friday”. -
/指定增幅
0/15在seconds 字段代表 “the seconds 0, 15, 30, and 45”.
5/15在seconds 字段代表 “the seconds 5, 20, 35, and 50”.
1/3在 day-of-month 字段代表从每个月的第一天开始,每隔三天触发一次”. -
L不同的字段有不同的含义.
day-of-month 字段的L代表一个月的最后一天,比如2月28 ;
day-of-week 字段的L,代表周六
day-of-week 字段,如果跟在其他的值后面,比如6L代表一个月的最后一个周五 . 你也可以用来表示倒数 比如L-3代表 一个月的倒数第三天,当你使用 ‘L’选项的时候,它不指定特殊的集合,或者数值范围,会让人很疑惑 -
W用来表示最接近给定值(入参)的工作日(周一到周五)
day-of-month字段值为15W,代表最接近这个月15日的工作日,如果15日是周六,那么14号的周五就会触发,如果是周日,那么会在16号周一触发。如果15t号是周三,会在15号当天触发 特殊情况, day-of-month字段的“1W” ,如果1号是周六,那么会在3号的周一触发,它不会跳过月份.W只能在Day of month字段指定
day-of-month字段的LW可以组合,代表一个月的最后一个工作日 -
#看示例吧,主要用在 day-of-week中
6#3代表一个月的第三个周五
2#1一个月的第一个周一
4#5一个月的第五个周五.
#5没有指定day-of-week字段将不会触发
不同场景的案例
重点关注day-of-week和day-of-month两个字段的*,?值
| Expression | Meaning |
|---|---|
| 0 0 12 * * ? | Fire at 12pm (noon) every day |
| 0 15 10 ? * * | Fire at 10:15am every day |
| 0 15 10 * * ? | Fire at 10:15am every day |
| 0 15 10 * * ? * | Fire at 10:15am every day |
| 0 15 10 * * ? 2005 | Fire at 10:15am every day during the year 2005 |
| 0 * 14 * * ? | Fire every minute starting at 2pm and ending at 2:59pm, every day |
| 0 0/5 14 * * ? | Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day |
| 0 0/5 14,18 * * ? | Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day |
| 0 0-5 14 * * ? | Fire every minute starting at 2pm and ending at 2:05pm, every day |
| 0 10,44 14 ? 3 WED | Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. |
| 0 15 10 ? * MON-FRI | Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday |
| 0 15 10 15 * ? | Fire at 10:15am on the 15th day of every month |
| 0 15 10 L * ? | Fire at 10:15am on the last day of every month |
| 0 15 10 L-2 * ? | Fire at 10:15am on the 2nd-to-last last day of every month |
| 0 15 10 ? * 6L | Fire at 10:15am on the last Friday of every month |
| 0 15 10 ? * 6L | Fire at 10:15am on the last Friday of every month |
| 0 15 10 ? * 6L 2002-2005 | Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 |
| 0 15 10 ? * 6#3 | Fire at 10:15am on the third Friday of every month |
| 0 0 12 1/5 * ? | Fire at 12pm (noon) every 5 days every month, starting on the first day of the month. |
| 0 11 11 11 11 ? | Fire every November 11th at 11:11am. |
参考网站
A Cron Expressions
详细! CronTrigger Tutorial
每天一个linux命令(50):crontab命令