如何将cron设置为每40分钟/25分钟运行一次脚本?

如何将cron设置为每40分钟/25分钟运行一次脚本?,cron,crontab,Cron,Crontab,我希望从第40分钟开始每40分钟运行一次脚本。 这意味着: 00:40, 01:20, 02:00, 02:40, 03:20... 因此,我向cron输入了以下内容: */40 * * * * /path/to/script/foo.sh 不幸的是,该脚本每40分钟运行一次: 00:40, 01:40, 02:40... 我打算每25分钟运行一次的脚本也是如此 我是不是遗漏了什么 答案 好的,万一你碰巧路过这里遇到同样的问题 我是这样解决的: # 40mins-interval 40

我希望从第40分钟开始每40分钟运行一次脚本。
这意味着:

00:40, 01:20, 02:00, 02:40, 03:20...
因此,我向cron输入了以下内容:

*/40 * * * * /path/to/script/foo.sh
不幸的是,该脚本每40分钟运行一次:

00:40, 01:40, 02:40...
我打算每25分钟运行一次的脚本也是如此

我是不是遗漏了什么


答案
好的,万一你碰巧路过这里遇到同样的问题
我是这样解决的:

# 40mins-interval
40 0 * * * /path/foo.sh         (0)
0,40 2-22/2 * * * /path/foo.sh  (2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
20 1-23/2 * * * /path/foo.sh    (1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23)  


# 25mins-interval
25,50 0 * * * /path/foo.sh              (0)
0,25,50 5-20/5 * * * /path/foo.sh       (5, 10, 15, 20)
15,40 1-21/5 * * * /path/foo.sh         (1, 6, 11, 16, 21)
5,30,55 2-22/5 * * * /path/foo.sh       (2, 7, 12, 17, 22)
20,45 3-23/5 * * * /path/foo.sh         (3, 8, 13, 18, 23)
10,35 4-19/5 * * * /path/foo.sh         (4, 9, 14, 19)
注:
1.此计划中仍将存在冲突(即:请参阅在两个时间间隔的第0分钟和第10分钟运行的计划)。

2.该脚本不会以从今天最后一次运行到第二天的精确间隔运行(即:25分钟间隔在今天23:45结束,第二天00:25开始)

它总是只拆分当前小时

40/40=1,因此它每40分钟运行一小时

*/5可以做5,10,15

你应该多休息一会儿

在25分钟的间歇时间里做*/30,在40分钟的间歇时间里每60分钟做一次

否则,为脚本设置两个crontab:

0,40 */2 * * * /path/to/script/foo.sh
20 1,3,5,7,9,11,13,15,17,19,21,23 * * * /path/to/script/foo.sh

对于要完成的任务,您必须在crontab中编写稍微复杂一点的条目

你看到上面的图案了吗

00:40,01:20,02:00,02:40,03:20和04:00,04:40,05:20,06:00,06:40,07:20,08:00

我可以将其分为三个条目:

  • 每一个偶数小时,你必须在第40分钟运行它
  • 每隔一个小时,你必须在20分钟时运行它
  • 每隔偶数小时,您必须在0上运行它。(0小时除外)
  • 您可以使用多个条目完成此操作:

    #1
    */40 0,*/2 * * * /path/to/script/foo.sh
    #2
    */20 1,*/2 * * * /path/to/script/foo.sh
    #3
    0 2,*/2 * * * /path/to/script/foo.sh
    
    注意:它可能有一些小问题,但我给了您方向:)


    PS:

    您将需要为同一脚本向cron添加多个条目,一个用于在小时内运行,一个用于二十过去,另一个用于二十到小时

    0 0,2,4,6,8,10,12,14,16,18,20,22 * * * script
    20 1,3,5,7,9,11,13,15,17,19,21,23 * * * script
    40 0,2,4,6,8,10,12,14,16,18,20,22 * * * script
    

    你说应该在00:40开始,但前一天的跑步时间是23:20。您想在午夜前后运行80分钟吗?

    如果您计算分钟数(、小时、天或周),您可以实现任意频率,因为,在脚本顶部添加一个条件,并将脚本设置为在crontab上每分钟运行一次:

    #! /bin/sh
    
    # Minute Cron
    # Usage: cron-min start
    # Copyright 2014 by Marc Perkel
    # docs at http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every_few_seconds_under_cron"
    # Free to use with attribution
    
    # Run this script under Cron once a minute
    
    basedir=/etc/cron-min
    
    if [ $# -gt 0 ]
    then
       echo
       echo "cron-min by Marc Perkel"
       echo
       echo "This program is used to run all programs in a directory in parallel every X minutes."
       echo
       echo "Usage: cron-min"
       echo
       echo "The scheduling is done by creating directories with the number of minutes as part of the"
       echo "directory name. The minutes do not have to evenly divide into 60 or be less than 60."
       echo
       echo "Examples:"
       echo "  /etc/cron-min/1      # Executes everything in that directory every 1  minute"
       echo "  /etc/cron-min/5      # Executes everything in that directory every 5  minutes"
       echo "  /etc/cron-min/13     # Executes everything in that directory every 13 minutes"
       echo "  /etc/cron-min/75     # Executes everything in that directory every 75 minutes"
       echo
       exit
    fi
    
    for dir in $basedir/* ; do
       minutes=${dir##*/}
       if [ $(( ($(date +%s) / 60) % $minutes )) -eq 0 ]
       then
          for program in $basedir/$minutes/* ; do
         if [ -x $program ]
         then
            $program &> /dev/null &
         fi
          done
       fi
    done
    
    #!/bin/bash
    
    minutesSinceEpoch=$(($(date +'%s / 60')))
    
    # every 40 minutes
    if [[ $(($minutesSinceEpoch % 40)) -ne 0 ]]; then
        exit 0
    fi
    
    返回当前日期,我们将其格式化为自历元起的秒(
    %s
    ),然后我们进行基本数学运算:

    # .---------------------- bash command substitution
    # |.--------------------- bash arithmetic expansion
    # || .------------------- bash command substitution
    # || |  .---------------- date command
    # || |  |   .------------ FORMAT argument
    # || |  |   |      .----- formula to calculate minutes/hours/days/etc is included into the format string passed to date command
    # || |  |   |      |
    # ** *  *   *      * 
      $(($(date +'%s / 60')))
    # * *  ---------------
    # | |        | 
    # | |        ·----------- date should result in something like "1438390397 / 60"
    # | ·-------------------- it gets evaluated as an expression. (the maths)
    # ·---------------------- and we can store it
    
    您可以将此方法用于每小时、每天或每月的cron作业:

    #!/bin/bash
    # We can get the
    
    minutes=$(($(date +'%s / 60')))
    hours=$(($(date +'%s / 60 / 60')))
    days=$(($(date +'%s / 60 / 60 / 24')))
    weeks=$(($(date +'%s / 60 / 60 / 24 / 7')))
    
    # or even
    
    moons=$(($(date +'%s / 60 / 60 / 24 / 656')))
    
    # passed since Epoch and define a frequency
    # let's say, every 7 hours
    
    if [[ $(($hours % 7)) -ne 0 ]]; then
        exit 0
    fi
    
    # and your actual script starts here
    

    读这个:谢谢!这很糟糕,但我想我现在必须把间隔的“手动”设置作为一个肮脏的补丁。我知道已经4年了,但我可能有一个解决25分钟cron的方法。您可以让cron每分钟运行一次(或每5分钟运行一次),脚本将检测25分钟是否已经过去。如果这是真的,那么你的脚本就可以完成它的任务了。在40分钟的时间间隔里,你为什么要跳过00:00?cron将在23:20和00:40运行,因此您跳过了一段时间间隔。感谢您的解释。然而,改变我的间歇时间是不可能的。我增加了一个40分钟间歇时间的变通方法。再多做一点工作,也可以在25分钟的时间间隔内完成。哇,这看起来好多了。非常感谢!:)这是:
    201,3,5,7,11,13,15,17,19,21,23***
    等于:
    201-23/2***
    ?谢谢!我现在就用这个。这很难看,但我需要马上修复它。这是一种有趣的方法,我喜欢shell代码。严格地说,这并没有回答如何设置cron以每40分钟/25分钟运行一次脚本的问题?因为它要求cron每分钟运行一次脚本。事实上,这在技术上并不正确,它是一种变通方法。为了澄清这一点,我最初提出这一点是为了解决受约束的环境,特别是OpenShift pre v3,创建cron作业的API将实际脚本文件放在以频率命名的文件夹中,如“.OpenShift/cron/minutely/”。