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
437 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now