【问题标题】:How does cron handle the "day" field?cron 如何处理“day”字段?
【发布时间】:2012-02-06 14:52:38
【问题描述】:

我正在尝试使用cron syntax 创建一个动态任务调度程序。

cron 如何处理超出该月天数的天数?例如,现在是二月,有 29 天。 cron 将如何处理日表达式 31*/2

如果 */2 扩展到 1,3,5..29,31 我可以看到 31 被删除。但如果 day 只是 31,那效果就不太好了。有什么想法吗?

【问题讨论】:

    标签: cron


    【解决方案1】:

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

    【讨论】:

    • 所以* * 31 * * 每年只能运行 7 个月?
    【解决方案2】:

    它会忽略它,因为它只使用一个简单的匹配。您可能正在尝试实现“每个月的最后一天运行”。像这样的东西应该可以工作(发现它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
    

    【讨论】:

    • 所以* * 31 * * 每年只能运行 7 个月?
    • 是的,仅限 31 天的月份。
    猜你喜欢
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多