unix/linux中的cron表达式是否允许指定确切的开始和结束日期

Kri*_*mar 15 unix linux cron crontab

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

  1. 我希望从2009年6月29日至2009年12月30日每天早上7点开始工作'X'.考虑当前日期为4/4/2009.

pax*_*blo 16

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

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

  • 一个是6月29日和30日("0 7 29,30 6 * X").
  • 7月到11月期间每天一个("0 7 * 7-11 * X").
  • 除了12月的最后一天("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
Run Code Online (Sandbox Code Playgroud)

然后确保在2010年6月29日之前将它们评论出来.您可以cron在12月31日添加最终作业,向您发送电子邮件,告知您需要将其禁用.

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

if [[ "$(date +%Y)" != "2009" ]] ; then
    exit
fi
Run Code Online (Sandbox Code Playgroud)

然后,如果您忘记禁用作业,则无关紧要.


Don*_*rve 5

是的,主要是。有些cron实现已经支持了很多年,有些则没有,所以我们假设您没有。另外,我假设此作业仅由cron守护程序运行,因此我们可以使用execute位来确定cron是否应运行该作业。

请注意,在您希望脚本运行之前,您需要将其保留为不可执行的脚本。

以下cron表达式可以满足您的要求(每天,包括周末)。根据需要进行调整:

# Make the job executable on 29 June.
0 6 29 6 * chmod +x /path/to/my/job/script

# Run the job between June and December, only if it's executable.
0 7 * 6-12 * test -x /path/to/my/job/script && /path/to/my/job/script

# Disable execution after 30 December.
0 8 30 12 * chmod -x /path/to/my/job/script
Run Code Online (Sandbox Code Playgroud)


And*_*c ॐ -1

您可以使用它来生成按特定时间间隔运行的 crontab:

http://www.robertplank.com/cron/

或这个

http://www.webmaster-toolkit.com/cron-generator.shtml

一种解决方案是设置 6 个 cron,每个月 1 个,每个在该月每天早上 7 点运行。

这可能是最简单的方法,下一种方法是编写脚本。