2012-11-27 128 views
0

我希望为我设置三个脚本的cron时间表提供一些帮助。我需要安排第一个脚本在每个月的第一个星期二运行,第二个脚本运行在每个月的第二个星期二,第三个脚本运行在每个月的第三个星期二。这是迄今为止我所拥有的。安排一个cron作业在每个月的第一,第二和第三个星期二运行

# Run first script on the 1st Tuesday of every month at 9:00 AM 
0 9 1,2,3,4,5,6,7 * 2 wget -q -O /dev/null http://site.com/first-script 

# Run second script on the 2nd Tuesday of every month at 9:00 AM 
0 9 8,9,10,11,12,13,14 * 2 wget -q -O /dev/null http://site.com/second-script 

# Run third script on the 3rd Tuesday of every month at 7:00 AM 
0 7 15,16,17,18,19,20,21 * 2 wget -q -O /dev/null http://site.com/third-script 

我相信这些脚本将在第一,第二,和每月的第三个星期二和每月的1-21日运行。从我读过的一周中的日子看来,日子是一个AND,这是真的吗?

希望这是可能的w/cron,否则我将不得不将决定运行脚本或不在脚本本身。

回答

0

另一种方法是创建主脚本以在每个星期二运行,验证星期二是和那个小时相应地调用相应的辅助脚本。

0 7,9 * * 2 wget -q -O /dev/null http://site.com/main-script 

我希望有帮助。

问候。

+0

很酷,谢谢。我想我会转移逻辑来确定每个星期二的PHP主要脚本,并简单地安排一个cron按照您的建议在每个星期二运行。谢谢! – scottystang

1

他们是OR。

它将运行每个星期二在上述日期。

2

您可以设置为cron的:

00 09 1-7,8-14,15-21 * 2 /路径/的MyScript

这将运行脚本上午9时1日,2日和第三个星期二。

0

如果您不想将日期检查逻辑直接放入您的脚本中,可以使用cron作业shell命令部分在执行脚本之前使用条件检查星期几。

# Run first script on the 1st Tuesday of every month at 9:00 AM 
0 9 1-7 * * [ "$(date '+\%a')" = "Tue" ] && wget -q -O /dev/null http://example.com/first-script 

# Run second script on the 2nd Tuesday of every month at 9:00 AM 
0 9 8-14 * * [ "$(date '+\%a')" = "Tue" ] && wget -q -O /dev/null http://example.com/second-script 

# Run third script on the 3rd Tuesday of every month at 7:00 AM 
0 7 15-21 * * [ "$(date '+\%a')" = "Tue" ] && wget -q -O /dev/null http://example.com/third-script 

如果[$(日期 '+ \%A')” = “星期二”]条件成功,将执行该脚本。%符号必须用反斜线因为cron的对待%作为一个特殊字符。

因为有7天,每周,每月的第一个星期二是保证在1-7范围内,第二次在8-14范围内,第三在15- 21范围

要捕获第一个星期二,你不能做到:

0 9 1-7 * 2 && wget -q -O /dev/null http://example.com/some-script 

...因为1-7(月日)和(星期)实际上是或运算。出于某种原因,克朗对这两个领域的看法与其他领域不同。在上面的例子中,你的脚本最终每天都会在每个星期二1-7范围内运行,

相关问题