2012-02-06 87 views
0

我正在试图创建一个动态的任务调度程序cron syntaxcron如何处理“日”字段?

cron如何处理月份中天数溢出的日期?例如,现在是2月份,29天。 cron如何处理当天的表情31*/2

如果*/2扩展为1,3,5..29,31我可以看到31被丢弃。但如果日期只是31,那就不太好。任何想法?

回答

0

我相信如果它不匹配,cron会忽略它。手写页面的写法说明,如果每月匹配或星期几匹配,它就会匹配。一些实现(例如cronie)只是将DoM中的*/2评估为1,3,5,7 ..,31,因此它在2月份必须忽略31。

+0

所以'* * 31 * *'只能运行7个月/年? – Znarkus 2012-02-06 15:22:32

1

它会忽略它,因为它只使用一个简单的匹配。你可能试图实现“每月最后一天运行”。像这样的东西应该工作(发现here):

59 23 * * * [ `date -d tomorrow +%d` -eq '01' ] && <your script> 

或者作出这样的cron来每天运行,却可以让它运行此脚本.SH:

TODAY=`date +%d` 
TOMORROW=`date +%d -d "1 day"` 

# If tomorrow date is less than today, we are at the end of the month 
if [ $TOMORROW -lt $TODAY ]; then 
    # This is the last day of the month, so you can do your stuff here...run other script... 
fi 
+0

所以'* * 31 * *'只能运行7个月/年? – Znarkus 2012-02-06 15:21:26

+0

是的,只有31天的月份。 – 2012-02-06 15:26:10