Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting to find multiple processes

Status
Not open for further replies.

smithware

IS-IT--Management
Mar 25, 2004
9
US
I need to use a monitoring tool to check for the existence of a handful of processes on an HP UX (11i?) box. The software can log on via SSH and run a script -
STDOut should be in the form of:

[SUCCESS|ERROR|UNCERTAIN]: {explanation} {DATA:<value>}

and I need to check that ALL of these processes are running:
icssvr
entserver
cservr
httpd
cos_bridge
ai_bridge

I have created a file called plist with the processes to be checked - my thoughts are along this line:

for i in `cat plist`
if [ ps -ea | grep $i == ``]
echo "ERROR: Process $i not running DATA: 0"
exit 1
fi
echo "SUCCESS: All processes running DATA: 1"
done
 
You can make use of the fact that grep returns 0 (i.e. success) if it finds a match, or 1 (i.e. failure) if it doesn't. The -q suppresses any output from grep.

Code:
for i in `cat plist`
do
    if ps -ea | grep -q $i
    then
        : # do nothing
    else
        echo "ERROR: Process $i not running  DATA: 0"
        exit 1
    fi
done
echo "SUCCESS: All processes running  DATA: 1"

Of course, with that logic it will only complain about the first process that it encounters that is not running, but it's a starting point.

Annihilannic.
 
Here's an example where you can pass the process name as an argument.

#!/bin/sh
# example: ./check_process icssvr

PROCESS_NAME=$1
# filter grep from results
NAME_FILTERS="grep"
# example of multiple filters with a | as a delimiter
#NAME_FILTERS="grep|java"

check_Process()
{
PROCESS_MATCH=`ps -ef|grep $PROCESS_NAME|egrep -v "$NAME_FILTERS"|wc -l`
if [ $PROCESS_MATCH -eq 0 ]
then
echo "ERROR: Process $PROCESS_NAME is not running DATA: $PROCESS_MATCH"
exit 1
else
echo "SUCCESS: Process $PROCESS_NAME is running DATA: $PROCESS_MATCH"
fi
}

check_Process

Rob

Rob Jordan
 
I had better luck with ps-ec instead of ps -ef

#!/bin/sh

set -x

PROCESS_NAME=$1

# filter grep from results
NAME_FILTERS="grep"
# example of multipe filters with a | as a delimeter
#NAME_FILTERS="grep|java"

check_Process()
{
PROCESS_MATCH=`ps -ec|grep $PROCESS_NAME|egrep -v "$NAME_FILTERS"|wc -l`
if [ $PROCESS_MATCH -eq 0 ]
then
echo "ERROR: Process $PROCESS_NAME is not running DATA: $PROCESS_MATCH"
exit 1
else
echo "SUCCESS: Process $PROCESS_NAME is running DATA: $PROCESS_MATCH"
fi
}

check_Process

Rob Jordan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top