9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

プロセスを監視するスクリプト

Posted at

linuxでデーモンを開発する場合なので、プロセスを監視するためにpsコマンドなどを手動で打って、生きているかどうか調べることがあります。毎回同じようなことを書いている気がするので、メモしておきます。

sample.sh

# !/bin/bash

function createReport()
{
        #ここで終了時に必要な処理を行う
        sleep 1
}

function isAlived()
{
        ps_name=$1
        isAlive=`pidof $ps_name | \
                 wc -l`
        return $isAlive
}

while true
do
        ps_name="a.out"
        isAlived $ps_name
        if [ $? -eq 0 ]; then
                echo "$ps_name is not alived"
                break
        fi
done

echo "report create -> start"
createReport
echo "report create -> end"

sleep 1
echo "finished ..."

このスクリプトでは無限ループでps コマンドを実行してプロセスがいるかどうかを調べて、プロセスがいなくなったら無限ループを抜けるようにしてあります。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?