2011-11-18 448 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