#-----------------------------------------------------------------------
#!/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
#---------------------------------------------------------------------
Antes de executar este script pela primeira vez, crie o arquivo /tmp/MSTRSvr.pid e adicione '999999999' (número aleatório) ao arquivo. O comando 'check' em 'else' falhará e você receberá um e-mail dizendo ' MSTRSvr foi reiniciado 'ignore isso e continue ...
Assim, cada script de intervalo verificará o PID e, em seguida, verificará o último PID conhecido.