Solved

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

Posted on 2009-07-07
17
447 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

0
Comment
Question by:conversekid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
17 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 480 total points
ID: 24794815
try this way

/usr/bin/cat $PROCESSFILE | while read PROCESS
do
set $PROCESS
test_proc
done
0
 
LVL 13

Assisted Solution

by:Rowley
Rowley earned 20 total points
ID: 24795053
use a for instead of while loop:

for PROCESS in `cat $PROCESSFILE`; do
test_proc $PROCESS
done
0
 

Author Comment

by:conversekid
ID: 24795590
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24795961
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
 

Author Comment

by:conversekid
ID: 24796548
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
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24796591
did you notice the change in

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

what is the value of LOGFILE_YN
0
 

Author Comment

by:conversekid
ID: 24796739
Hi, If if [ "$LOGFILE_YN" == "y" ];  then value of LOGFILE_YN is also y. No change in its value in the loop.
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24796782
Then this will always display the message

ERROR: PROCESS NOT RUNNING! - The process with PID  is not running!
0
 

Author Comment

by:conversekid
ID: 24796850
oh..so what would you llike me to do?
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24796887
can you comment it or remove it and see if you get the script run correct?
0
 

Author Comment

by:conversekid
ID: 24796987
oops....still the same..
0
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24797010
can you post the script (complete one)?
0
 

Author Comment

by:conversekid
ID: 24797169
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
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24797473
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
 

Author Comment

by:conversekid
ID: 24797740
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
 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 480 total points
ID: 24797921
did you copy my script as I posted? it is running fine on my system
0
 

Author Comment

by:conversekid
ID: 24798041
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question