特定のコマンドを 1 時間半ごとに実行するように cron を設定するにはどうすればよいですか?
10 に答える
通常の単一の式では不可能cron
です。
コードを変更せずにできる最善の方法は次のとおりです。
0 0,3,6,9,12,15,18,21 * * * [cmd]
30 1,4,7,10,13,16,19,22 * * * [cmd]
必要な cron のバージョンによっては、これらは圧縮可能である可能性があります。
0 */3 * * * [cmd]
30 1-23/3 * * * [cmd]
1時間や2時間を使えない正当な理由はありますか?確かにもっと簡単でしょう。
私はこれを個人的に試したことはありませんが、cron を 90 分ごとに実行する方法については、こちらを参照してください: http://keithdevens.com/weblog/archive/2004/May/05/cron
上記のリンクからの抜粋:
0 0,3,6,9,12,15,18,21 * * * <commands>
30 1,4,7,10,13,16,19,22 * * * <commands>
crontab の 2 行。次の行に沿って:
0 0,3,6,9,12,15,18,21 * * * /usr/bin/foo
30 1,4,7,10,13,16,19,22 * * * /usr/bin/foo
2 つの crontab エントリでそれを行うことができます。それぞれが 3 時間ごとに実行され、次のように 90 分ずつオフセットされます。
0 0,3,6,9,12,15,18,21 * * *
30 1,4,7,10,13,16,19,22 * * *
次のようなより複雑な時間仕様も受け入れるfcronを使用することもできます。
@ 01h30 my_cmd
#! /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/90 # Executes everything in that directory every 90 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
*/10 * * * * root perl -e 'exit(time()%(90*60)>60)' && command
90 — 分で表すと 1 時間半です
"> 60" — スクリプトの開始を 1 分遅らせる cron 機能を提供します
また、このハックの助けを借りて、分単位で任意の期間を設定できます
たとえば、71 分ごとにスクリプトを開始します。
* * * * * root perl -e 'exit(time()%(71*60)>60)' && command
Epochから分 (時間、日、または週) をカウントし、スクリプトの先頭に条件を追加し、スクリプトを crontab で毎分実行するように設定すると、任意の頻度を達成できます。
#!/bin/bash
minutesSinceEpoch=$(($(date +'%s / 60')))
# every 90 minutes (one and a half hours)
if [[ $(($minutesSinceEpoch % 90)) -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
私のcrontabに以下を追加し、動作しています
15 */1 * * * root /usr/bin/some_script.sh >> /tmp/something.log