Script to test for scripts running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to test for scripts running
# 1  
Old 08-13-2008
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  
Old 08-13-2008
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  
Old 08-14-2008
Cheers. Im trying to learn how to script so am keen to get this working.

Thanks for the pointer though.
# 4  
Old 08-14-2008
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  
Old 08-14-2008
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  
Old 08-14-2008
Quote:
Originally Posted by runnerpaul
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
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
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
By the way, thanks for all the help so far.
no problem
# 7  
Old 08-15-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running test command in ssh

I tried to run the following commands in my shell script. This command works fine for me: ssh -n "${username}"@"${hostname}" "grep WARNING ${serverhome}/${serverlog} | wc -l" The result is: 1548 However when i try to run a similar one: ssh -n "${username}"@"${hostname}" "test `grep -q... (2 Replies)
Discussion started by: hys1117
2 Replies

2. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

Heyas, Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p The last point for motivation was... (17 Replies)
Discussion started by: sea
17 Replies

3. Shell Programming and Scripting

Backup script / Test if script is already running

Hello everyone, I have 2 questions : 1) I have a backup shell script, let's call it backup.sh, that is called every hour as a cron job. As a matter of fact a backup could last more than one hour. It mounts a NAS and then do some rsync on important directories, so really I don't want to... (2 Replies)
Discussion started by: freddie50
2 Replies

4. Infrastructure Monitoring

Test if Oracle listener is running

Hi All , I am new to shall scripting, i want write an script for oracle tns and listener. If tns working i want o/p as "Listener and TNS are working" else o/p should be ""Listener and TNS are not Working" below is the command in unix to check the tns status, if no output it means TNS... (3 Replies)
Discussion started by: abdul bari
3 Replies

5. Programming

running PLSQL scripts through shell script

I am running the following ealth checks on my server there are two databases in my server . MODEL1 and MODEL2 i connect with the first database as sqlplus model1/password Then i exceute a query select x from table (4 Replies)
Discussion started by: asalman.qazi
4 Replies

6. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

7. Shell Programming and Scripting

Running scripts via su

Hi All, Am using the below command to start my application using the root user su - bin -c "/home/bin/test/start.sh" but am getting the error becaue i have set some environment varibales in bin .profile when i execute the command start.sh by logging directly into bin account it's... (8 Replies)
Discussion started by: ravi.sri24
8 Replies

8. Linux

running test

-------------------------------------------------------------------------------- I am running a test command and if the user is not found you have to type exit and it wiil exit. Ok I did that. But if the user is found it is supposed to finger them. But my problem is it makes you type exit first... (2 Replies)
Discussion started by: skooly5
2 Replies

9. Shell Programming and Scripting

Running SQL Scripts from Shell script - Need insight!

I've a script that fetches various values from the database as below: #! /bin/ksh $conn="user/pwd@service_name" `sqlplus -s << $conn EOF1 @xyz.sql @pqr.sql @abc.sql EOF1` The output of the script should generate txt files containing the results from queries which are further... (6 Replies)
Discussion started by: manthasirisha
6 Replies

10. UNIX for Dummies Questions & Answers

Script to Test Application Server is running

Hi, I'm a complete novice at Unix and need to create a script that does the following... checks to see if an application server is running. If the app is running then print 'Available' Else print 'Unavaliable' exit from scriopt I have no idea where to start. I'd be very grateful... (0 Replies)
Discussion started by: duglover
0 Replies
Login or Register to Ask a Question