【问题标题】:Cron expression to run every day starting from a date从某个日期开始每天运行的 Cron 表达式
【发布时间】:2016-04-28 18:09:31
【问题描述】:

我需要一个 cron 表达式,它会从 2016 年 1 月 25 日开始每天下午 12 点触发。这就是我想出的:

0 0 12 25/1 * ? *

但在 1 月 31 日之后,下一次发射时间是 2 月 25 日。

是否有用于执行此操作的 cron 表达式?如果不是,我可以使用什么?

【问题讨论】:

    标签: cron quartz-scheduler cronexpression


    【解决方案1】:

    假设您希望在 1 月 25 日之后永远运行此进程(即 2032 年,届时服务器可能已经被替换),我将使用三个表达式来完成:

    0 0 12 25-31 1 * 2016  command # Will run the last days of Jan 2016 after the 25th
    0 0 12 * 2-12 * 2016   command # Will run the rest of the months of 2016
    0 0 12 * * * 2017-2032 command # will run for every day of years 2017 and after.
    

    我希望这会有所帮助。

    【讨论】:

    • 我正在使用石英调度程序,它说您的 cron 表达式无效。
    • 谢谢!对不起。我没有注意到石英在表达式格式中添加了第二个。我用你所期望的可以在石英中工作的东西编辑了答案
    【解决方案2】:

    有多种方法可以完成此任务,一种是运行带有 cron 作业和测试条件的脚本,如果真正运行实际需要脚本,则跳过。

    这是一个例子,

    20 0 * * * home/hacks/myscript.sh
    

    并在 myscript.sh 中将您的代码放入测试条件并运行实际的命令/脚本

    这是一个这样的脚本的例子,

    #!/bin/bash
    
    if( ( $(date) <= "31-01-2016" ) || ( $(date) >= "25-02-2017" ) ){
    
       // execute your command/script
    }else {
         // do Nothing
    }
    

    【讨论】:

    • 您的示例在语法上无效,或者至少,您的语法与您希望的完全不同。
    【解决方案3】:

    您可以编写一个仅匹配特定时间点之后的日期的日期表达式;或者您可以为您的脚本创建一个包装器,如果当前日期早于主脚本应该运行的时间,则该包装器会中止

    #!/bin/bash
    # This is GNU date, adapt as required for *BSD and other variants
    [[ $(date +%s -d 2018-02-25\ 00:00:00) > $(date +%s) ]] && exit
    exec /path/to/your/real/script "$@"
    

    ...或者您可以使用 at 安排添加此 cron 作业。

    at -t 201802242300 <<\:
    schedule='0 0 12 25/1 * ? *'   # update to add your command, obviously
    crontab=$(crontab -l)
    case $crontab in
        *"$schedule"*) ;;          # already there, do nothing
        *) printf "%s\n" "$crontab" "$schedule" | crontab - ;;
    esac
    :
    

    (未经测试,但你明白了。我只是复制/粘贴了你的时间表达式,我想它对crontab 并不真正有效。我认为 Quartz 有办法做类似的事情。)

    at 的时间规范很奇怪,我设法让它在 Mac 上工作,但在 Linux 上可能会有所不同。请注意,我将其设置为在前一天晚上 23:00 运行,即计划的第一次执行前一小时。

    【讨论】:

      【解决方案4】:

      这是我的回答 here 的简短副本。 最简单的方法是使用额外的脚本来进行测试。你的 cron 看起来像:

      # Example of job definition:
      # .---------------- minute (0 - 59)
      # |  .------------- hour (0 - 23)
      # |  |  .---------- day of month (1 - 31)
      # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
      # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
      # |  |  |  |  |
      # *  *  *  *  *   command to be executed
        0  12  *  *  *  daytestcmd 1 20160125 && command1
        0  12 *  *  *   daytestcmd 2 20160125 && command2
      

      这里,command1 将从 2016 年 1 月 25 日起每天执行。 command2 将从 2016 年 1 月 25 日起每隔一天执行一次。

      daytestcmd 定义为

      #!/usr/bin/env bash
      # get start time in seconds
      start=$(date -d "${2:-@0}" '+%s')
      # get current time in seconds
      now=$(date '+%s')
      # get the amount of days (86400 seconds per day)
      days=$(( (now-start) /86400 ))
      # set the modulo
      modulo=$1
      # do the test
      (( days >= 0 )) && (( days % modulo == 0))
      

      【讨论】:

        猜你喜欢
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多