I got a simple script working to check if a process is running and then email if it is not running anymore. My scenario is that I need to make sure the process is always running so instead of running the below script via cron I think it is better to a have a looping script to check continually. I'm unsure on this and don't want to bodge it up and create a burden to the system.
I did search but couldn't get a match that helped me. Can anyone point me at a sample I can edit please?
Cheers
#!/bin/csh
# This script checks to see if process e_1 is up, if down it sends an email. If up it exits.
#
set DATE=`date +%d%m%y`
set TIME=`date +%H%M`
set RECIPIENT="email@"
set DPID = `ps -ef | grep -v grep | grep e_1`
if ("$DPID" == "") then
echo "The e_1 process went down at $TIME on $DATE" | mail -s "The u_1 process went down" $RECIPIENT
else
echo "e_1 is up"
endif
exit
If this loops forever, then you will get hundreds of emails per minute when the process is down. We have scripts very like this - we run them in cron every 15 minutes.
a loop to check a process is ruuning, if it is then no action, if it isn't then start the process and at that point send an email to say that it has been re-started.
then go back to start of the loop.
Yes, if it can
restart and check the process 3 times before sending an email 'Process wont run'
Put a sleep command in there at the bottom of the loop
failure_count=0;
while true
do
# your code here
# end of your code
if [[ $failure_count -gt 2 ]] ; then
echo "Can't run process X" |\
/usr/bin/mailx -s 'Process X failed' me@myaddress.com mybackup@hisaddress.com
failure_count=0
fi
sleep 10
done
You need to increment the failure_count when you have succesive failures, otherwise set it to zero each time.
thanks for your help. i'm writing this in csh as i have to use a source command to be able to start a process within a certain environment. all the other scripts on the server are also csh so i'm not sure if I can change to sh or kcsh.
I'm not really a scripter so am a bit unsure how to get the failure count to work. i have tried both of these but they just get stuck on the expression is true part.
sorry for being dumb.
this one is checking DPID to see if it IS NOT empty.
set DPID = `ps -ef | grep -v grep | grep e_1`
set FAILURE_COUNT="0"
while ("$DPID" != "")
echo "e_1 is up"
if ("$FAILURE_COUNT" > "2") then
echo "test"
failure_count=0
endif
sleep 10
end
this one is checking DPID to see if it IS empty.
set DPID = `ps -ef | grep -v grep | grep e_1`
while ("$DPID" != "")
echo "e_1 is up"
if ("$DPID" == "") then
echo " test"
endif
sleep 10
end
hi, just to confirm that i started the u_1 process before running these scripts and killed them in a different terminal so that i could monitor what they were doing. they only did echo "e_1 is up"
i was using using the grep below as it only returns something if it finds it.
ps -ef | grep -v grep | grep e_1
The grep below returns the process if it is running as well as the process of grep itself. Therefore this grep will always return something so I can't test if it IS NOT empty.
ps -ef | grep "ue_21"
When using a simple if statement my grep does work. sorry if i am missing something.
Hi, thanks for the feedback. I did note the [] brackets in th grep. Here is what I have now.
When I run this it loops but keeps echoing ue_21 is up even when i stop the ue_21 process. I am checking that it isn't re-starting it as well. The script doesn't get to the echo " test" part. Sorry if this is obvious but I am really scratching my head at this as it looks correct. cheers if anyone has any ideas as to where i am going wrong.
set DATE=`date +%d%m%y`
set TIME=`date +%H%M`
set RECIPIENT="test@test.co.uk"
set DPID = `ps -ef | grep "e_21"`
while ("$DPID" != "")
echo "ue_21 is up"
if ($DPID == "") then
echo " test"
echo "The ue_21 utility went down at $TIME on $DATE" and was told to restart | mail -s "The ue_2
1 utility went down" $RECIPIENT
endif
sleep 10
end
i made some changes and it appears to be working now so it may be of help to someone out there;
#!/bin/csh
set DATE=`date +%d%m%y`
set TIME=`date +%H%M`
set RECIPIENT="test@test.co.uk"
while (`ps -ef | grep -c "[u]e_21"` == "1")
echo "ue_21 is up"
sleep 10
if (`ps -ef | grep -c "[u]e_21"` == "0") then
echo "The ue_21 utility went down at $TIME on $DATE" and was told to restart | mail -s "The ue_21 utility went down" $RECIPIENT
endif
end