Given a list of process names I need a solaris bash script to monitor if they are running or not

I am trying to write a script to which takes process names from a file and checks if it is running or not..When I try check for a single process it works. However when I acutally include in a loop it is not working. Please advice.


test_proc () {
if [[ "$1" = "" ]] ; then echo "hey, give me an argument" ; return 1; fi
if [ -z `/usr/ucb/ps auwwx| grep java|grep ${PROCESSNAME} |grep TEST | awk '{print $2}'` ]; then
  echo "\n`tput smso`ERROR: PROCESS NOT RUNNING!`tput rmso` - The process with PID $PID is not running!"
   if [ "$LOGFILE_YN" = "y" ]; then
     echo "ERROR: The process with PID $PID is not running on host $HOST at $DATE $TIME!\n" >>$LOGFILE
   fi
   else
  echo "The process $PROCESS is ok."
 fi
 
}
 
I am calling this function using :
 
/usr/bin/cat $PROCESSFILE | while read PROCESS
do
test_proc $PROCESS
done

Open in new window

conversekidAsked:
Who is Participating?
 
omarfaridConnect With a Mentor Commented:
try this way

/usr/bin/cat $PROCESSFILE | while read PROCESS
do
set $PROCESS
test_proc
done
0
 
RowleyConnect With a Mentor Commented:
use a for instead of while loop:

for PROCESS in `cat $PROCESSFILE`; do
test_proc $PROCESS
done
0
 
conversekidAuthor Commented:
Hi, Still the same.. The issue I am facing here is to pass the value of the PROCESSNAME variable into the following line in the function. In the function if I do echo $PROCESSNAME it returns a value, however in the if statement when I try to pass its value to grep it dosent take it for some reason.

 if [ -z `/usr/ucb/ps auwwx| grep java|grep ${PROCESSNAME} |g
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
omarfaridConnect With a Mentor Commented:
I don't know what the script is trying to do with too many grep commands but I came with my own :)

#!/usr/bin/ksh
test_proc () {
if [ "$1" == "" ] ; then echo "hey, give me an argument" ; return 1; fi
if [ -z `/usr/ucb/ps auwwx| grep -v PID | grep ${PROCESSNAME} | awk '{print $2}'` ]; then
  echo "\n`tput smso`ERROR: PROCESS NOT RUNNING!`tput rmso` - The process with PID $PID is not running!"
   if [ "$LOGFILE_YN" == "y" ]; then
     echo "ERROR: The process with PID $PID is not running on host $HOST at $DATE $TIME!\n" >>$LOGFILE
   fi
   else
  echo "The process $PROCESS is ok."
 fi
 
}

for PROCESSNAME in `cat $PROCESSFILE`
do
  test_proc $PROCESSNAME
done
0
 
conversekidAuthor Commented:
Hi omarfarid,

Thanks for your respose.. pls see below:

[root@e08k41:/export/home/dwr763] #> ksh -x asd.ksh
+ PROCESSFILE=/export/home/dwr763/process.txt
+ cat /export/home/dwr763/process.txt
+ test_proc asis1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc autoaudit1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc bcsproductreg1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc ceasir1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc chart1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc compass1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc dashboard1

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
+ test_proc dimOtasu1
^C
[root@e08k41:/export/home/dwr763] #> /usr/ucb/ps auwwx| grep -v PID  | grep asis1 | awk '{print $2}'
375
394
6215
6233
16749
[root@e08k41:/export/home/dwr763] #> /usr/ucb/ps auwwx| grep java|grep  asis1 | awk '{print $2}'
394

I am not understanding why the value of process is not being recognized when passed into the loop...
0
 
omarfaridConnect With a Mentor Commented:
did you notice the change in

 if [ "$LOGFILE_YN" == "y" ]; then

what is the value of LOGFILE_YN
0
 
conversekidAuthor Commented:
Hi, If if [ "$LOGFILE_YN" == "y" ];  then value of LOGFILE_YN is also y. No change in its value in the loop.
0
 
omarfaridConnect With a Mentor Commented:
Then this will always display the message

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
0
 
conversekidAuthor Commented:
oh..so what would you llike me to do?
0
 
omarfaridConnect With a Mentor Commented:
can you comment it or remove it and see if you get the script run correct?
0
 
conversekidAuthor Commented:
oops....still the same..
0
 
omarfaridConnect With a Mentor Commented:
can you post the script (complete one)?
0
 
conversekidAuthor Commented:
Hi..Pls find the script below
#!/bin/bash
#This script monitors list of processes.
# last change: 31.05.2006
# check for bacmonit
#if [ -f /usr/ucb/whoami ]; then
##ID=$(/usr/ucb/whoami)
#if [ "$ID" != "bacmonit" ]; then
#  echo "\nERROR: $ID, you must be bacmonit to run this program."
#   exit 1
# fi
# VARIABLES
LOGFILE_YN="n"
PROCESSFILE="/export/home/dwr763/process.txt"        # enter filename and full path of PROCESS FILE
LOGFILE="/export/home/dwr763/psmon.log"
HOST=`uname -n`
DATE=`date +'%d.%m.%Y'`
TIME=`date +'%k:%M Uhr'`
printf "\nWebLogic process monitoring script ,\n\n Please take necessary action for the processes that are not running /export/home/bacmonit/psmon.log"
#
# FUNCTIONS
#
function test_proc ()
if [[ "$1" = "" ]] ; then echo "hey, give me an argument" ; return 1; fi
test_proc () {
if [ "$1" == "" ] ; then echo "hey, give me an argument" ; return 1; fi
if [ -z `/usr/ucb/ps auwwx| grep -v PID | grep ${PROCESSNAME} | awk '{print $2}'` ]; then
  echo "\n`tput smso`ERROR: PROCESS NOT RUNNING!`tput rmso` - The process with PID $PID is not running!"
   if [ "$LOGFILE_YN" == "y" ]; then
     echo "ERROR: The process with PID $PID is not running on host $HOST at $DATE $TIME!\n" >>$LOGFILE
   fi
   else
  echo "The process $PROCESS is ok."
 fi
}
 
# main
if [ -n "$PROCESSFILE" ]; then
 [[ -r "$PROCESSFILE" ]] || { echo "\n ERROR: FILE NOT FOUND! - PROCESSFILE $PROCESSFILE was not found!" ; exit 1 ; }
fi
/usr/bin/cat $PROCESSFILE | while read PROCESS
do
  test_proc $PROCESSNAME
done
#mailx -s "Weblogic process monitoring on `hostname`" dwr763@motorola.com < /export/home/bacmonit/psmon.log
exit 0;
 
 
[root@e08k41:/export/home/dwr763] #> more process.txt
asis1
autoaudit1
bcsproductreg1
ceasir1
chart1
compass1
dashboard1
dimOtasu1
ebutton1
eintel91
ematrix1
extreg1
grro91
iCare1
idenHome1
idm1
itrp1
jira1
mdtool

Open in new window

0
 
omarfaridConnect With a Mentor Commented:
I did some changes please try this


#!/bin/bash
#This script monitors list of processes.
# last change: 31.05.2006
# check for bacmonit
#if [ -f /usr/ucb/whoami ]; then
##ID=$(/usr/ucb/whoami)
#if [ "$ID" != "bacmonit" ]; then
#  echo "\nERROR: $ID, you must be bacmonit to run this program."
#   exit 1
# fi
# VARIABLES
LOGFILE_YN="n"
PROCESSFILE="/export/home/dwr763/process.txt"        # enter filename and full path of PROCESS FILE
LOGFILE="/export/home/dwr763/psmon.log"
HOST=`uname -n`
DATE=`date +'%d.%m.%Y'`
TIME=`date +'%k:%M Uhr'`
printf "\nWebLogic process monitoring script ,\n\n Please take necessary action for the processes that are not running /export/home/bacmonit/psmon.log"
#
# FUNCTIONS
#
function test_proc ()
if [[ "$1" == "" ]] ; then echo "hey, give me an argument" ; return 1; fi
test_proc () {
if [ "$1" == "" ] ; then echo "hey, give me an argument" ; return 1; fi
if [ -z "`/usr/ucb/ps auwwx| grep -v PID | grep ${PROCESSNAME} | awk '{print $2}'`" ]; then
  echo "\n`tput smso`ERROR: PROCESS NOT RUNNING!`tput rmso` - The process with PID $PID is not running!"
   if [ "$LOGFILE_YN" == "y" ]; then
     echo "ERROR: The process with PID $PID is not running on host $HOST at $DATE $TIME!\n" #>>$LOGFILE
   fi
   else
  echo "The process $PROCESSNAME is ok."
 fi
}
 
# main
if [ -n "$PROCESSFILE" ]; then
 [[ -r "$PROCESSFILE" ]] || { echo "\n ERROR: FILE NOT FOUND! - PROCESSFILE $PROCESSFILE was not found!" ; exit 1 ; }
fi
/usr/bin/cat $PROCESSFILE | while read PROCESSNAME
do
  test_proc $PROCESSNAME
done
#mailx -s "Weblogic process monitoring on `hostname`" dwr763@motorola.com < /export/home/bacmonit/psmon.log
exit 0;

Open in new window

0
 
conversekidAuthor Commented:
Thanks much...I am afraid I am still getting the error below..

 Please take necessary action for the processes that are not running /export/home/bacmonit/psmon.log\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
\nERROR: PROCESS NOT RUNNING! - The process with PID  is not running!


*****

[root@e08k41:/export/home/dwr763] #> /usr/ucb/ps auwwx| grep -v PID | grep asis1 | awk '{print $2}'
375
394
6215
6233
23399
0
 
omarfaridConnect With a Mentor Commented:
did you copy my script as I posted? it is running fine on my system
0
 
conversekidAuthor Commented:
Hi omarfarid. Thanks much for your time and your quick response.I checked on my second test server and your script works absolutely fine.....
0
All Courses

From novice to tech pro — start learning today.