Cron 表达式 Author: Arjen Poutsma
┌───────────── second (0-59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of the month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (0 - 7)
│ │ │ │ │ │ (0 or 7 is Sunday, or MON-SUN)
│ │ │ │ │ │
* * * * * *
适用规则:
-
一个字段可以是 星号(
*
),它总是代表 “first-last”(所有值)。对于 “day of month”(月的多少号) 或 “day of week”(星期几) 字段,可以使用问号?
而不是星号。 -
数字的范围由两个用 连字符(
-
) 分隔的数字表示。指定包含的范围。 -
在
范围(或 * )
后面加上/n
,表示间隔,(在该范围内 指定数字的值 的 间隔)。 -
“
month
”和“day of week
”字段可以使用对应的英文名称。使用特定日期或月份的前三个字母(大小写无关紧要)。如 JAN,MON -
“
day of month
”和“day of week
”字段可以包含一个L 字符
,代表“last
”,在每个字段中有不同的含义:- 在“
day of month
”字段中,L
代表“这个月的最后一天
”。如果后面跟着一个负偏移量
(即L-n
),则表示“从一个月的第n天到最后一天
”。如果后面跟着W
(即LW
),则表示“这个月的最后一个工作日
”。 - 在“
day of week
”字段中,L
代表“一周的最后一天
”。如果 前缀是数字或三个字母 ( 如dL
或DDDL
),则表示“该月d周(或DDD)的最后一天
”( DDD为英文名称的前三个字母 )。
- 在“
-
"
day of month
"字段可以是nW
,表示 “the nearest weekday to day of the month n
”(月中最近的工作日n
),如果 nW的结果落在 周六,那么nW的结果应是 这个周六前的周五。如果nW的结果落在 周日,那么nW的结果应是 这个周日后的周一(即下周一),如果n是1,落在周六也会发生这种情况(即1W
表示“the first weekday of the month
” )。 -
“
day of week
”字段可以是d#n
(或DDD#n
),表示 “the n-th day of week d (or DDD) in the month
”(一个月中d周(或DDD)的第n天
)。
示例表达式:
-
"
0 0 * * * *
" = the top of every hour of every day.每天的每一个小时
-
"
*/10 * * * * *
" = every ten seconds.每隔10秒
-
"
0 0 8-10 * * *
" = 8, 9 and 10 o’clock of every day.每天的 8,9,10 整点
-
"
0 0 6,19 * * *
" = 6:00 AM and 7:00 PM every day.每天的 上午6点 和 下午7点
-
"
0 0/30 8-10 * * *
" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.每天的8点到10点间,每隔30分钟 (即 每天的8:00, 830, 9:00, 9:30, 10:00 10:30)
-
"
0 0 9-17 * * MON-FRI
" = on the hour nine-to-five weekdays在工作日(周一到周五)早9点到晚5点的时间
-
"
0 0 0 25 12 ?
" = every Christmas Day at midnight每个圣诞节的午夜
-
"
0 0 0 L * *
" = last day of the month at midnight这个月的最后一天午夜
-
"
0 0 0 L-3 * *
" = third-to-last day of the month at midnight这个月倒数第三天的午夜
-
"
0 0 0 1W * *
" = first weekday of the month at midnight这个月的第一个工作日午夜
-
"
0 0 0 LW * *
" = last weekday of the month at midnight这个月最后一个工作日的午夜
-
"
0 0 0 * * 5L
" = last Friday of the month at midnight这个月最后一个星期五的午夜
-
"
0 0 0 * * THUL
" = last Thursday of the month at midnight这个月最后一个星期四的午夜
-
"
0 0 0 ? * 5#2
" = the second Friday in the month at midnight这个月第二个星期五的午夜
-
"
0 0 0 ? * MON#1
" = the first Monday in the month at midnight这个月的第一个星期一午夜
还支持以下宏命令
- “
@year
”(或“@annual
”)每年运行一次,即"0 0 0 1 1 *
" - "
@month
"每月运行一次,即“0 0 0 1 * *
” - “
@weekly
”每周运行一次,即“0 0 0 * * 0
” - “
@daily
”(或“@midnight
”)每天运行一次,即:“0 0 0 * * *
” - “
@hour
”每小时运行一次,即:"0 0 * * * *
"