web-dev-qa-db-ja.com

40分/ 25分ごとにスクリプトを実行するようにcronを設定するにはどうすればよいですか?

40分から40分ごとにスクリプトを実行したいと思います。
つまり、次のことを意味します。

00:40, 01:20, 02:00, 02:40, 03:20...

だから私はこのエントリをcronに作成しました:

*/40 * * * * /path/to/script/foo.sh

残念ながら、これは1時間の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に開始します)。

17
cr8ivecodesmith

常に現在の時間を分割します。

40/40 = 1なので、1時間の40分ごとに実行されます。

*/5は5、10、15を実行します...

あなたはより大きな間隔で行くべきです。

25分間隔では*/30を実行し、40分間隔では60分ごとに実行します。

それ以外の場合は、スクリプトに2つの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
14
Udo Held

実行したいタスクについては、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

私はそれを3つのエントリに分けることができます:

  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: これはもっとたくさん説明します

5
BlackDivine

同じスクリプトの複数のエントリをcronに追加する必要があります。1つは正時に実行するためのもの、1つは過去20回用、もう1つは20時間までです。

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分のギャップが必要ですか?

4

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
_
2
stefanmaric
#! /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
0
user3174711