unix/linux 中的 cron 表达式是否允许指定确切的开始和结束日期 [英] Does cron expression in unix/linux allow specifying exact start and end dates

查看:12
本文介绍了unix/linux 中的 cron 表达式是否允许指定确切的开始和结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够配置这样的东西.

I want to be able to configure something like this.

  1. 我想从 2009 年 6 月 29 日到 2009 年 6 月 30 日每天早上 7 点运行作业X".将当前日期视为 2009 年 4 月 4 日.

推荐答案

可以以一种棘手的方式完成.

It can be done in a tricky sort of way.

对于该范围,您需要三个单独的 cron 作业,都运行相同的代码(在本例中为 X):

You need three separate cron jobs for that range, all running the same code (X in this case):

  • 6 月 29 日和 30 日的一个 ("0 7 29,30 6 * X").
  • 七月到十一月的每一天(0 7 * 7-11 * X").
  • 除了 12 月的最后一天之外的所有其他内容(0 7 1-30 12 * X").
  • one for the 29th and 30th of June ("0 7 29,30 6 * X").
  • one for every day in the months July through November ("0 7 * 7-11 * X").
  • one for all but the last day in December ("0 7 1-30 12 * X").

这给你:

# Min   Hr   DayOfMonth   Month   DayOfWeek   Command
# ---   --   ----------   -----   ---------   -------
   0     7      29,30        6        *          X
   0     7          *     7-11        *          X
   0     7       1-30       12        *          X

然后确保在 2010 年 6 月 29 日之前将它们注释掉.您可以在 12 月 31 日添加最终的 cron 作业,通过电子邮件通知您需要禁用它.

Then make sure you comment them out before June 29, 2010 comes around. You can add a final cron job on December 31 to email you that it needs to be disabled.

或者你可以修改 X 如果不是 2009 年,则立即退出.

Or you could modify X to exit immediately if the year isn't 2009.

if [[ "$(date +%Y)" != "2009" ]] ; then
    exit
fi

那么忘记禁用作业也没关系.

Then it won't matter if you forget to disable the jobs.

这篇关于unix/linux 中的 cron 表达式是否允许指定确切的开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆