2

Below script checks whether MStrsvr process is running or not . The issue I'm facing is if i schedule a cron tab to run this script for every 1 hour it will throw email alert every 1hr that "MSTRSvr is running" which i don't want . I want the script to throw alert only when server is stopped/started.

#!/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
4
  • Remove the code in the else part?
    – user79743
    Feb 28, 2016 at 16:30
  • if i remove the code in else path when the process is restarted it wont throw alert that "the process is running " ..! thats the challenge im facing here . 1.process stop --> email alert should be triggered 2.when process is started again--> email alert should be triggered only once .rest of the time when process is running it should not throw email alert .
    – star
    Feb 28, 2016 at 16:40
  • Then store last state in a file inside /tmp/ and add a check for the content of that file.
    – user79743
    Feb 28, 2016 at 16:47
  • i have never tried it before ..can post a sample syntax to do it ?
    – star
    Feb 28, 2016 at 17:00

2 Answers 2

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
#---------------------------------------------------------------------

Before running this script for the first time, create the /tmp/MSTRSvr.pid file and add '999999999'(random number) to the file, The check under 'else' command will fail and you will receive email saying 'MSTRSvr was restarted' ignore that and continue...

So every interval script will check for the PID and then checks it against the last known PID.

4
  • thanks you so much . could you please explain what this line does ? kill -0 cat /tmp/MSTRSvr.pid > /dev/null 2>&1 will it kill the original MSTRSvr process ?
    – star
    Feb 28, 2016 at 17:41
  • No, signal number 0 will not kill the process, it will return 0 if the process is active and non-zero if it is not. Feb 28, 2016 at 18:11
  • thank you for sharing these many information's.last help is it a good practice to use temporary .txt files and then remove it using rm command ? is there any other good practice to store temporary information in a file and remove it . (like how i stored all information in wa.txt and mi.txt and sent it as a mail )
    – star
    Feb 29, 2016 at 13:43
  • It depends on situation, If this is a critical operation I would write tmp file in to a folder where only you have access to as /tmp is accessible by all the users and there is good chance file getting deleted or modified. Feb 29, 2016 at 15:18
0

Add a test for the last state of the server:

#!/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

    echo "stopped" > "filewithlaststate.txt"

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

else

    if [ "$(cat "filewithlaststate.txt")" != "running" ]
    then 
         echo "MSTRSvr is running" >>mi.txt

         echo "running" > "filewithlaststate.txt"

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

fi

rm wa.txt 2>ni.txt
rm mi.txt 2>ni.txt
1
  • thank you for sharing me another way of approaching this issue .
    – star
    Feb 29, 2016 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .