2012-02-06 50 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