admin管理员组

文章数量:1565833

[译] Cron Trigger Tuorial

0.前言

本文译自: http://www.quartz-scheduler/documentation/quartz-2.x/tutorials/crontrigger.html
之所以想翻译本篇文章,因为在Linux的Crontab和Azkaban中需要使用schedule的功能,但是这个功能对于新手来说,还是稍有难度。这里便给出一些新手入门指南。

CronTrigger 教程
1.简介

cron 是一个已经存在很久的Unix工具,所以它的schedule能力既强大,又稳定。
CroTrigger使用cron expressions,这个是可以创建执行schedule,比如:周一到周五的每天早上8点,或者每个月最后一个周五的下午1:30。
cron expressions强大但是却可能稍微令人疑惑。这个教程只在揭开构建一个cron expression的迷纱。

2.格式[Format]

一个cron expression由空格分割的6-7个字段构成。字段可以包含任何允许设置的字段,同时也可以将各种允许的字段结合在一起。允许的字段如下:

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 	, - * /
3.特殊字符[Special characters]
  • *:(所有可能值)-被用于在域中选择所有值。例如:在分钟字段中的“*”就意味着每一分钟
  • ?:(不指定值)-当你需要指定某事在俩个字段中,这个字符是被允许的,而不是其他。例如,如果我想执行命令在某月指定的日期下执行(比如说,每月的10号),但是并不在意这一天是该周的哪一天,那么我将在day-of-month域中存放10,在day-of-week域中存放?。如果需要仔细研究,请看下面的例子。
  • /: 经常用于指定增量。例如,在秒的设计域中使用“0/15”就意味着:秒0,15,30,45。并且,秒域中的"5/15"就是命令会在秒数为5,20,35,50,处执行…day-of-month区域的’1/3’意味着:在每个月的第一天起,每3天生效一次。
  • L:在它所允许的不同域中有着不同的意义。例如,在day-of-month域中的L意思就是“该月的最后一天”——一月的31号,平年2月的28号…如果这个字段仅用在day-of-week中,那么仅仅意味着7或SAT(周六)。但是如果它用在day-of-week域中,并且位于另外的一个值之后,则意味着“一个月的最后第L天”——例如:"6L"就意味着“这个月的最后一个星期五”。同时,你也可以指定一个到每月最后一天的偏移量,诸如“L-3”意思就是:每月的倒数第三天。当使用L选项时,重要的是:不要指定列表、值区间,如果你那么做的话,你可能会得到意想不到的结果。
  • W:常用于指定离给定日期最近的工作日(Monday-Friday)。下面就是一个示例。如果你在day-of-month区域指定的值是15W,意思就是:“距离本月15号最近的一个工作日”,比如说,假设该月15日是周六,那么这个最近的日子则是周五(也就是说在14日);如果该月15日是周日,则距离周日最近的日子就是周一(也就是在16号)。如果该月15号就是一个工作日,那么就是15号就是目标日期。然而如果你指定1W作为day-of-month区域的值,并且假设当该月第一天是周六的话,则真正的触发日期是星期一(3号)。因为cron不会跨越月份取工作日。W字符仅仅指定在day-of-month域中的某一天,而不能是一个天数区间、列表。
  • L W 字符能在day-of-month区域中组合形成LW,这个即是:某月的最后一周。
  • #:在每月的天数里用来指定第几个。例如,在day-of-week中的值6#3意思就是:该月的第三个周五(day 6 = Friday 且“#3” = 该月的第三个)。再看其他的例子:“2#1”指的就是该月的第一个周一;“4#5”指的就是该月的第5个周三。注意:如果你指定“#5”,并且在day-of-week所在的month中,没有第五个指定工作日,那么在该月不会执行程序。

合法的字符以及月份名字、days of week均不区分大小写。MON 和mon是相同的。

下面提供一些例子:其中的字段分别是seconds minutes hours dayOfMonth month dayOfWeek year

**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.
4.Notes

1.支持day-of-week 以及 day-of-month域中的值是不完整的。(你现在必须在其中之一的域中填写?)

2.当执行启动时间在凌晨时分,当时间制在你的地区发生了改变时,(对于美国locale,这通常是在凌晨2点之前和之后的一个小时,因为时间的变化会导致跳跃或重复,这取决于时间是向后移动还是向前跳跃)。通过如下链接:你可以在Wikipedia中找到有用的信息去帮助你确认你所在的具体时区:https://secure.wikimedia/wikipedia/en/wiki/Daylight_saving_time_around_the_world

本文标签: 详解Azkabanschedule