【问题标题】:How can I create Cron Expression which works between particular times如何创建在特定时间之间工作的 Cron 表达式
【发布时间】:2018-09-25 19:44:38
【问题描述】:

我正在为一项必须在工作日从早上 7.30 到早上 8.20(含)每 10 分钟运行一次的作业编写 Cron 表达式。

我使用的触发器:- 0 30/10 7-9 ? * 周日、周一、周二、周三、周四 *

问题是它是否也适用于 8.20 之后?我们可以将其限制在特定的分钟或结束时间吗?比如7:30-8:20

【问题讨论】:

    标签: cron


    【解决方案1】:

    有些事情你不能用一个表达式来做,你可以考虑使用两个:

    # 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
    # This runs on 7:30, 7:40, 7:50
     30/10    7  *  * 1-5  command
    # This runs on 8:00, 8:10, 8:20
     0-20/10  8  *  * 1-5  command
    

    您可以尝试的另一种方法是使用测试。

    # 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
    */10    7,8  *  * 1-5  testcmd && command
    

    testcmd 是一个可执行脚本,看起来像:

    #!/usr/bin/env bash
    hours=$(date "+%H")
    minutes=$(date "+%M")
    (( hours == 7 && minutes >= 30)) || (( hours == 8 && minutes <= 20 ))
    

    这个技巧的其他例子可以在这里找到:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2023-01-09
      • 2017-08-26
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2011-05-18
      相关资源
      最近更新 更多