infinite loop to check process is running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting infinite loop to check process is running
# 1  
Old 10-16-2008
infinite loop to check process is running

Hi,

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


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

# 2  
Old 10-16-2008
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.
# 3  
Old 10-16-2008
hi jim.

thanks for your reply. would a better model be?

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.

cheers
mark
# 4  
Old 10-16-2008
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
Code:
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.
# 5  
Old 10-17-2008
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.
Code:
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.
Code:
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

# 6  
Old 10-17-2008
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"

cheers
# 7  
Old 10-17-2008
Quote:
Originally Posted by yabai
Code:
set DPID = `ps -ef | grep -v grep | grep e_1`

Useless use of grep
Code:
set DPID = `ps -ef |  grep "[e]_1"`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select command going to infinite loop after running the script

cd /opt/et/WAS/apps/8.0 find . -name "HostIntegration.properties" -o -name "HostSocket.properties" -o -name "environment.properties" 2> /dev/null | awk -F '' '{print $4}'|awk '!x++' | cat>/home/cbadmin/file1.txt cd /home/cbadmin/ PS3='Please enter a number from list of applications==>:' select... (3 Replies)
Discussion started by: bhas85
3 Replies

2. Shell Programming and Scripting

Infinite while loop script shows more than one process

Hi, I have a script which triggers an infinite loop. #!bin/bash trig=`ls /home/trig.tch |wc -l` function callj { some commands... } while do callj & done The number of process after doing a ps -ef |grep Mon.sh returns processes even after the script is killed by deleting the... (4 Replies)
Discussion started by: chetan.c
4 Replies

3. UNIX for Dummies Questions & Answers

How a process can check if a particular process is running on different machine?

I have process1 running on one machine and generating some log file. Now another process which can be launched on any machine wants to know if process1 is running or not and also in case it is running it wants to stream the logs file generated by process1 on terminal from which process2 is... (2 Replies)
Discussion started by: saurabhnsit2001
2 Replies

4. Shell Programming and Scripting

need to check if the process is running

Hi, I check if the process is running or not using the below. /usr/ucb/ps auxww | grep 109 |grep rmi | awk '{print $2}' 9718 Thus we see 9718 is the PID. It return blank if the process is not running. I need to perform some action if the process is not running and leave it if... (8 Replies)
Discussion started by: shifahim
8 Replies

5. Programming

How do I check if a process is running in C

How to I check if a process is running in C? I'm trying to use ps aux |grep "process name" but failing in doing that. How do I do that? Thanks, (1 Reply)
Discussion started by: cprogdude
1 Replies

6. UNIX and Linux Applications

How to check if process is running?

Hi I would like to check if an instance of a script is already running. I've seen many different examples, but I haven't the slightest idea as to how to do this. Can you please help. Thank you. (5 Replies)
Discussion started by: ladyAnne
5 Replies

7. Shell Programming and Scripting

Check if Process is running

Hi , I have a csh code below which check the process if it's running. Can any expert advise me on the following: 1) what does this notationmean ">!" and how is it different from the append ">" notation ? 2) how does "setenv" work in this code ? # Check whether there is a running... (3 Replies)
Discussion started by: Raynon
3 Replies

8. Shell Programming and Scripting

loop when process running

Hi Gurus, Could you please help me to create a shell script that will be started by a cron job once every night at 24.00 h (that should bee easy:)) The shell script should control every 30 seconds the name of a process, and when the process doesn't run anymore it should execute a few further... (12 Replies)
Discussion started by: blackwire
12 Replies

9. Shell Programming and Scripting

Running a script in INFINITE LOOP

Hi All, I have a requirement as below. I supposed to get a file from Source system once in a month. But we dont know when the source system will send the file. My script has to wait for that file in LOOP once it gets the file then it has to FTP the file. I thought of scheduling the job... (5 Replies)
Discussion started by: Raamc
5 Replies

10. Shell Programming and Scripting

check process running

I have this script: ------------------------------------------------------- #!/bin/ksh # if ] || ] then echo "Executing main_load.sh script" /usr1/psc_load/jobs/cron/main_load.sh "ods" else echo "File not found, do nothing" fi exit 0 ... (4 Replies)
Discussion started by: rose1207
4 Replies
Login or Register to Ask a Question