We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

conversekid
conversekid asked
on
Medium Priority
474 Views
Last Modified: 2013-12-27
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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...
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi, If if [ "$LOGFILE_YN" == "y" ];  then value of LOGFILE_YN is also y. No change in its value in the loop.
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
oh..so what would you llike me to do?
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
oops....still the same..
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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
CERTIFIED EXPERT
Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.....
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.