キック用
kick_allalone.sh
#!/bin/sh
for i in `seq 1 10`
do
echo -n "[`date '+%Y/%m/%d %T'`] $i kick allalone.sh"
sh ./allalone.sh &
sleep 1
done
スクリプト
allalone.sh
#!/bin/sh
# 自身が起動していたら終了
if [ $$ != `pgrep -fo $0` ]; then
echo "[`date '+%Y/%m/%d %T'`] myself is already running. exit myself."
exit 1
fi
# debug code
echo "[`date '+%Y/%m/%d %T'`] hoge 1st"
sleep 3
exit 0
実行結果
ekaneko@bibian ~/work/allalone % sh kick_allalone.sh
[2014/05/29 15:11:55] 1 kick allalone.sh[2014/05/29 15:11:55] hoge 1st
[2014/05/29 15:11:56] 2 kick allalone.sh[2014/05/29 15:11:56] myself is already running. exit myself.
[2014/05/29 15:11:57] 3 kick allalone.sh[2014/05/29 15:11:57] myself is already running. exit myself.
[2014/05/29 15:11:58] 4 kick allalone.sh[2014/05/29 15:11:58] hoge 1st
[2014/05/29 15:11:59] 5 kick allalone.sh[2014/05/29 15:11:59] myself is already running. exit myself.
[2014/05/29 15:12:00] 6 kick allalone.sh[2014/05/29 15:12:00] myself is already running. exit myself.
[2014/05/29 15:12:01] 7 kick allalone.sh[2014/05/29 15:12:01] hoge 1st
[2014/05/29 15:12:02] 8 kick allalone.sh[2014/05/29 15:12:02] myself is already running. exit myself.
[2014/05/29 15:12:03] 9 kick allalone.sh[2014/05/29 15:12:03] myself is already running. exit myself.
[2014/05/29 15:12:04] 10 kick allalone.sh[2014/05/29 15:12:04] hoge 1st
ekaneko@bibian ~/work/allalone %
解説
忙しくて泣いてる時に同僚にお願いして作ってもらいました。
どこにでもあるコードだそうです。
かなり重い処理をcronで定期的に起動してる場合、終わらないのに起動しちゃうなんて事を防げます。
お陰で考慮しなきゃいけない大きな事項が一つ減らせたので大変助かりました。
メインのスクリプトに仕込むのではなく、メインスクリプトの起動用スクリプトに仕込むと汎用性が上がります。