ubichupas.net

Upstartでプロセス管理した際のメモ

Upstartの動作確認をするため、まずログレベルを「message」から「debug」に変更しておきます。
# initctl log-priority
message
# initctl log-priority debug
# initctl log-priority
debug

# tail -f /var/log/messages

ジョブ定義ファイル(/etc/init/*.conf)にはデーモン起動前に実行するpre-start scriptと、デーモン起動後に実行するpost-start scriptを記載できます。

起動タイミングとなるイベントにはinitctl emitコマンドで発行できる任意のイベント名の他、ランレベル変更時に発行される「runlevel」イベント、別のジョブ定義ファイルのpre-start scriptが開始した際に発行される「starting ジョブ名」イベント、post-start scriptが完了した際に発行される「started ジョブ名」イベントがあります。 複数のイベントを括弧と「and」「or」で組み合わせることも可能です。 ちなみにstart onを複数記載しても最後の行しか有効になりませんでした。

停止タイミングは同様にstop onを記載し、pre-stop scriptと「stopping ジョブ名」、post-stop scriptと「stopped ジョブ名」があります。


起動タイミングに「and」を使用した場合、先に発行したイベントがいつまで有効なのか判然としないため、環境変数を駆使してpre-start scriptで起動判定した方が複雑な制御を実現しやすそうでした。 initctl startで起動した場合に$UPSTART_EVENTSが未定義になる点も見逃せません。

start on MY_START or YOUR_START
stop on MY_STOP

expect fork
exec /usr/local/sbin/mydeamon02
respawn

pre-start script
        if [ -z "$UPSTART_EVENTS" ]
        then
                exit 0
        fi
        if [ ${STEP:-0} -ne 2 ]
        then
                exit 1
        fi
end script

post-start script
        if [ -n "$UPSTART_EVENTS" ]
        then
                initctl emit $UPSTART_EVENTS STEP=3 --no-wait
        fi
end script

if文の書き方は/bin/shのリンク先に依存します。(この場合はbash)