如何设置cron每40分钟/ 25分钟运行我的脚本?

时间:2011-11-18 11:50:48

标签: 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 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)。

5 个答案:

答案 0 :(得分:14)

它总是只分割当前小时。

40/40 = 1所以它每隔40分钟运行一次。

* / 5会做5,10,15 ......

你应该去更大的间隔。

* 25分钟间隔* / 30,间隔40分钟每60分钟。

否则为脚本设置两个crontabs:

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

答案 1 :(得分:5)

对于你想要完成的任务,你必须在你的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

我可以把它分成三个条目:

  1. 每隔一小时你必须在第40分钟运行
  2. 每个奇数小时你必须在第20分钟运行
  3. 每隔一小时你必须在0(<0小时除外)上运行
  4. 您可以使用多个条目完成此操作:

    #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:This will explain alot more

答案 2 :(得分:3)

您需要为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分钟吗?

答案 3 :(得分:2)

如果计算自Epoch以来的分钟(,小时,天或周),在脚本顶部添加条件,并将脚本设置为在crontab上每分钟运行一次,则可以达到任何频率:

#!/bin/bash

minutesSinceEpoch=$(($(date +'%s / 60')))

# every 40 minutes
if [[ $(($minutesSinceEpoch % 40)) -ne 0 ]]; then
    exit 0
fi

date(1)返回当前日期,我们将其格式化为自Epoch(%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 :(得分:0)

#! /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