2011-11-18 140 views
9

我想要一个脚本从第40分钟开始每40分钟运行一次。
这样就意味着:如何设置cron每40分钟/ 25分钟运行脚本?

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第二天开始)以确切间隔运行。

+1

阅读:http://stackoverflow.com/questions/745901/how-to-do-a-cron-job-每72分钟 –

+1

谢谢!这很糟糕,但我想我现在必须做间隔的“手动”设置,作为一个肮脏的修复。 – cr8ivecodesmith

+0

我知道这已经4年了,但我可能有解决方案的25分钟cron。 你可以让cron每分钟运行一次(或每5分钟..),如果25分钟过去了,脚本会检测到它。如果这是真的,那么你的脚本就会完成它的工作。 – Kayla

回答

10

它总是只拆分当前小时。

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

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

你应该去大间隔。

为你的25分钟间隔做* * 30,40分钟间隔每60分钟做一次。

否则为您的脚本2级的crontab:

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

感谢您的解释。但是,改变我的间隔是不行的。 – cr8ivecodesmith

+0

我为40分钟的时间间隔添加了解决方法。再多花点功夫也能在25分钟的时间内工作。 –

+0

哇,这看起来好多了。谢谢一堆! :) – cr8ivecodesmith

4

对于要完成你必须写在crontab多一点点复杂项任务。

你看到上面的模式?

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

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

  1. 你拥有的每甚至小时,在第40分钟
  2. 运行它,你有,每一个奇数小时,在20分钟运行
  3. 每甚至小时,你必须在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:This will explain alot more

+0

谢谢!我现在就去。这很丑陋,但我需要立即修复这个问题。 – cr8ivecodesmith

0

您需要为cron添加几个条目,一个用于运行小时,一个用于二十岁,另一个用于tw适合于小时。

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分钟的差距吗?

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 
2

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

#!/bin/bash 

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

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

date(1)返回当前的日期,我们因为大纪元格式设置为秒(%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