特定の時間に実行して終了するBashスクリプトの作成

特定の時間に実行して終了するBashスクリプトの作成

次のようなスクリプトを作成したいと思います。一日のうちの特定の時間に始まり、別の特定の時間に終わります。

たとえば、テストしたいプログラムがあるため、スクリプトは午後10時に起動し、午前9時まで実行し続けるように設定されています。

これは、プログラムを継続して実行することについて私が持っていた別の質問の続きです。

私は以下を持っています:

#!/bin/bash

trap "echo manual abort; exit 1"  1 2 3 15;
RUNS=0;
while open  -W /Path/to/Program.app
do
    RUNS=$((RUNS+1));
    echo $RUNS > /temp/autotest_run_count.txt;
done

exit 0

このスクリプトはデフォルトでMac OSXで私のプログラムを実行し、エラーをキャッチします。それ以外の場合は、プログラムが閉じたときにプログラムを再実行します。

上記のように実行したいと思います。午後10時に始まります。午前9時に終了します。

あなたのアドバイスは常に便利です。

ありがとうございます!

ユデン

ベストアンサー1

起動時に一度実行でき、午後9時から午前9時の間にタスクを実行できるようにスクリプトを拡張しました。

#!/bin/bash -·
LOGFILE="/tmp/autotest_run_count.txt"

trap "echo manual abort; exit 1"  1 2 3 15
RUNS=0
while [ 1 ] ; do·
    HOUR="$(date +'%H')"
    if [ $HOUR -ge 21 -a $HOUR -lt 9 ] ; then
        # run program
        libreoffice || exit 1
        RUNS=$((RUNS+9))
        echo $RUNS > $LOGFILE
    else
        echo $RUNS, waiting H=$HOUR > $LOGFILE
        # note: calculating the time till next wakeup would be more 
        # efficient, but would not work when the time changes abruptly
        # e.g. a laptop is suspended and resumed
        # so, waiting a minute is reasonably efficient and robust
        sleep 60
    fi
done

おすすめ記事