プロセスステータスが変更されたときにのみ電子メール通知を送信するスクリプト

プロセスステータスが変更されたときにのみ電子メール通知を送信するスクリプト

次のスクリプトは、MStrsvrプロセスが実行されていることを確認します。私が直面している問題は、このスクリプトを1時間ごとに実行するようにクローンタブをスケジュールすると、1時間ごとに「MSTRSvrが実行中です」という電子メール警告が送信されることです。サーバーが停止/起動したときにのみスクリプトに警告するようにしたいと思います。

#!/bin/ksh
hos=$(hostname)

curr_Dt=$(date +"%Y-%m-%d %H:%M:%S")

var=$(ps -ef | grep -i '[/]MSTRSvr')

if [ -z "$var" ]
then

    echo "ALERT TIME : $curr_Dt" >>wa.txt
    echo "SERVER NAME : $hos" >>wa.txt
    echo "\n \n" >>wa.txt
    echo " MSTRSvr is not running on $hos Please check for possible impact " >>wa.txt
    echo "\n \n" >>wa.txt

    mail -s "MSTRSvr process ALERT" [email protected] <wa.txt

else

    echo "MSTRSvr is running" >>mi.txt

    mail -s "MSTRSvr process ALERT" [email protected] <mi.txt

fi

rm wa.txt 2>ni.txt
rm mi.txt 2>ni.txt

ベストアンサー1

#-----------------------------------------------------------------------
#!/bin/ksh

hos=$(hostname)
curr_Dt=$(date +"%Y-%m-%d %H:%M:%S")

# I am going to get the process ID for the MSTRSvr.
ProcessPID=$(ps -ef | grep -i '[/]MSTRSvr' | grep -v grep | awk '{print $2}') 

if [[ -z ${ProcessPID} ]]; then
    # There is no PID, Not running!
    echo "ALERT TIME : $curr_Dt" >>wa.txt
    echo "SERVER NAME : $hos" >>wa.txt
    echo "\n \n" >>wa.txt
    echo " MSTRSvr is not running on $hos Please check for possible impact " >>wa.txt
    echo "\n \n" >>wa.txt
    mail -s "MSTRSvr process ALERT" [email protected] <wa.txt
else
    # The process is running check it against the last recorded PID.
    # You can also compare /tmp/MSTRSvr.pid with ${ProcessPID}.
    kill -0 `cat /tmp/MSTRSvr.pid` > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
       # The current PID does not match.
       echo "MSTRSvr was restarted." >>mi.txt
       # Update the tempfile with current running PID.
       echo ${ProcessPID}>/tmp/MSTRSvr.pid
       mail -s "MSTRSvr process ALERT" [email protected] <mi.txt
    fi
fi

rm wa.txt 2>ni.txt
rm mi.txt 2>ni.txt
#---------------------------------------------------------------------

このスクリプトを最初に実行する前に/tmp/MSTRSvr.pidファイルを生成し、ファイルに「999999999」(任意の数字)を追加すると、「else」コマンドの下の確認が失敗し、「MSTRSvrが再起動されました」 「無視して続行してください...

したがって、各間隔スクリプトはPIDをチェックし、最後に知られているPIDをチェックします。

おすすめ記事