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
Automatically Running Scripts jeffreydavisjr UNIX for Dummies Questions & Answers 4 11-19-2008 05:05 AM
running test skooly5 Linux 2 04-07-2008 12:22 PM
running multiple scripts nvuradi Shell Programming and Scripting 3 08-13-2007 09:53 AM
Running SQL Scripts from Shell script - Need insight! manthasirisha Shell Programming and Scripting 6 07-03-2006 10:55 PM
Script to Test Application Server is running duglover UNIX for Dummies Questions & Answers 0 04-28-2004 09:47 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 08-13-2008
runnerpaul runnerpaul is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 15
Script to test for scripts running

Hi,

Im writing a script that will check which scripts are running. The script will run on a 5min loop and the status of the scripts will be written to the log. If any of the scripts arent running an email will be sent out.

At the min if all scripts are running an entry is made to the log saying so. If any are down an entry is made to the log saying tis and an email is sent out.

The problem is that the email sent out just sends out the name of the last script in AllScripts.txt(even if it is not down). I wish to have the script compare which scripts are not running against the list in my file and include these scripts in the email.

Ideally I would also like to list the scripts that are down in my log but I figure if I can get one working I'll be able to do the other.

Any info/help would be appreciated cos I have hit a bit of a block.

Heres what I got so far:-

chk_scripts.sh:
Code:
log() {
    /usr/bin/echo "$1" >> ${LOGFILE}
}

checkstatus(){
   serverName=`grep $i $confFile|sed 's/^[   ]*\([^=  ]*\)[=    ].*$/\1/g'`
   pid=`ps -efa | grep $serverName|grep -v 'grep '|sed 's/^[^0-9]*\([0-9]*\)[^0-9].*$/\1/'`
   if [ "$pid" = "" ]
   then
      srvr_status="N"
   else
      srvr_status="Y"
   fi
}
ChkScript(){
 confFile="/var/tmp/Paul/scripts/AllScripts.txt"
 for i in `cat $confFile`
  do
     myScript=`echo $i|sed 's/^[   ]*\([^=  ]*\)[=    ].*$/\1/g'`
     checkstatus
     if [ $srvr_status="N" ]
     then
        ScriptList="$myScript "
     fi
  done
     #log "ERROR : $ScriptList not Running !!!!"
     if [ $mail_count -le 10 ]
        then
        echo "$ScriptList not running!!!!" | /usr/lib/sendmail paul@me.com
        mail_count=`expr $mail_count + 1`
     fi
}
##################################################################
LOGFILE=/var/tmp/Paul/scripts/chk_myscripts.log
SCRIPTNAME="chk"
mail_count=0
while true
do
  ScriptList=""
  lpaprocess=`ps -efa |grep $SCRIPTNAME |grep -v 'grep ' |wc -l`
  if [ $lpaprocess -eq 4 ]
 then
     mail_count=0
     log "All CHK Scripts Running"
  elif [$lpaprocess -lt 4 ]
 then
  log "CHK script not running!!!!"
  #ProcList="FAL"
  ChkScript
  fi
 sleep 300
done
fi
AllScripts.txt:
Code:
/var/opt/moto/live/scripts/chk_pending.sh
/var/opt/moto/live/scripts/chk_process.sh
/var/opt/moto/live/scripts/chk_log_test1.sh
/var/opt/moto/live/scripts/chk_log_test2.sh
Cheers
Paul
  #2 (permalink)  
Old 08-13-2008
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
i know is not the perfect answer, but i think you better check out hobbit script for procs
it does exactly what you want, even better, you could use the client
The Hobbit Monitor
  #3 (permalink)  
Old 08-14-2008
runnerpaul runnerpaul is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 15
Cheers. Im trying to learn how to script so am keen to get this working.

Thanks for the pointer though.
  #4 (permalink)  
Old 08-14-2008
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
fair enough.
i think you are over working with that script.

i would do like this (psudo code)
Code:
make a full ps and save it to a temp file.
for each line in the AllScripts.txt file
      grep the line we just read in the ps output
      if we found it
            send some ok or just do nothing
      if we dont find it
            mail -s we are under atack superman@gmail.com
for how actually do all that stuff. well, i can think of these

PS
ps -whatevarOptionsForYourUnix > /tmp/tempile


"for each line" <-- i would use a rediction to a while
Code:
while read line
do
    echo "line is a var wich contains one single line read fomr the file, including newline"
    echo " this is $line"
done < /var/tmp/Paul/scripts/AllScripts.txt
i think im in love with the "feeding from below" thingy :P

"grep the line"
just use grep $line /tmp/tempfile

"if we found it"
check for greps erturn code. i think that goes liek this
0 - grep found at least something
1= grep did not find anything
and you check if with
if [ $? -eq 0 ]
$? holds the return code of the last program/subshell spawned
-eq is an option for test that is read "equals" and aplies to integers

that would be the basic skeleton.
the decicion structures and stuff.
you can read alot more about bash and scripting here.
(Ħincluding test, internal bash variables, and redictions)
Advanced Bash-Scripting Guide
  #5 (permalink)  
Old 08-14-2008
runnerpaul runnerpaul is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 15
Cheers. Im looking into that.
At the min im writing my grep to output to a tmp file. I can get this working but includes my actual grep in the output too. I know -v is used to hide the grep but not sure how it should look in a script. Heres what I got:

Code:
ps -efa | grep chk > /var/tmp/Paul/scripts/tmp
I have tried:

Code:
`ps -efa | grep chk -v > /var/tmp/Paul/scripts/tmp`
and:

Code:
`ps -efa | grep chk | grep -v 'grep '` > /var/tmp/Paul/scripts/tmp
Both created an empty tmp file.

Got any suggestions?

By the way, thanks for all the help so far.
  #6 (permalink)  
Old 08-14-2008
broli's Avatar
broli broli is offline
Registered User
  
 

Join Date: Dec 2007
Location: Argentina
Posts: 215
Quote:
Originally Posted by runnerpaul View Post
Cheers. Im looking into that.
At the min im writing my grep to output to a tmp file. I can get this working but includes my actual grep in the output too. I know -v is used to hide the grep but not sure how it should look in a script. Heres what I got:

Code:
ps -efa | grep chk > /var/tmp/Paul/scripts/tmp
why you grep for chk?
Quote:
Originally Posted by runnerpaul View Post
I have tried:

Code:
`ps -efa | grep chk -v > /var/tmp/Paul/scripts/tmp`
ofcorse, the way to doit is how you did it next
Quote:
Originally Posted by runnerpaul View Post
and:

Code:
`ps -efa | grep chk | grep -v 'grep '` > /var/tmp/Paul/scripts/tmp
Both created an empty tmp file.

Got any suggestions?
ig the first grep donst find chk, then the second one is taking out the only match.
Quote:
Originally Posted by runnerpaul View Post
By the way, thanks for all the help so far.
no problem
  #7 (permalink)  
Old 08-15-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Are you really using backticks in those commands? Take them out, they don't do what you want.

As an aside, the customary way to avoid a Useless Use of Grep is to grep for a regex which doesn't match itself.

Code:
ps -efa | grep '[c]hk' >/path/to/tempfile
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 10:55 PM.


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