The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check for statup process in loop? dataciph3r Shell Programming and Scripting 1 09-25-2008 09:18 PM
Check whether ftpd process is running or not? The.White.Rider SUN Solaris 8 06-17-2008 07:51 AM
check process running rose1207 Shell Programming and Scripting 4 12-28-2007 01:23 AM
Infinite Loop in Autosys while running a shell script, Manual run is fine sharmagaurav_2k Shell Programming and Scripting 2 09-04-2007 08:20 AM
How to check if another instance of the process is running sim Shell Programming and Scripting 8 06-30-2005 07:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-16-2008
yabai yabai is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
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 (permalink)  
Old 10-16-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,723
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 (permalink)  
Old 10-16-2008
yabai yabai is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
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 (permalink)  
Old 10-16-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,723
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 (permalink)  
Old 10-17-2008
yabai yabai is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
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 (permalink)  
Old 10-17-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,421
Quote:
Originally Posted by yabai View Post
Code:
set DPID = `ps -ef | grep -v grep | grep e_1`
Useless use of grep
Code:
set DPID = `ps -ef |  grep "[e]_1"`
  #7 (permalink)  
Old 10-20-2008
yabai yabai is offline
Registered User
  
 

Join Date: May 2008
Posts: 11
hi danmero,

i was using using the grep below as it only returns something if it finds it.
Code:
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.
Code:
ps -ef | grep "ue_21"
When using a simple if statement my grep does work. sorry if i am missing something.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0