Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

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

0
conversekid
Asked:
conversekid
  • 8
  • 8
9 Solutions
 
omarfaridCommented:
try this way

/usr/bin/cat $PROCESSFILE | while read PROCESS
do
set $PROCESS
test_proc
done
0
 
RowleyCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
omarfaridCommented:
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
 
omarfaridCommented:
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
 
omarfaridCommented:
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
 
omarfaridCommented:
can you comment it or remove it and see if you get the script run correct?
0
 
conversekidAuthor Commented:
oops....still the same..
0
 
omarfaridCommented:
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
 
omarfaridCommented:
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
 
omarfaridCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now