Script to check status of a PID

i'm just learning scripting, and have been banging my head against this

I want to check if my WAS6 java process is running and if so..echo me a messages. If not then echo me a different messages

the problem i have is I dont know how to represent a NULL return value. If i grep for a was6 java pid, the $? is always '0'...no matter if a PID came back or not. any idea on how to write/rewrite this??

PID=`/usr/ucb/ps -auxwww |grep -i [s]erver1 |grep -v grep | awk '{print $2}'`

    if [ $? -eq  ] ; then \( i have also tried \`if [ $PID -eq ] ; then\) 

echo " WAS6 is down,please restart"
else
echo " WAS6 is running, no action needed"
fi
--------------------------------------------------------------------

In addition, i've tried this variation below, which seems like works ..but i always get the error when executing..

# cat test.ksh
#!/bin/ksh
#
# Check to see if URLs respond in a given time period
#
BASE_DIR="/local/apps/adminutils"
WGET="$BASE_DIR/bin/wget"
DAT_FILE="$BASE_DIR/etc/url_check.dat"
MAIL_LIST="user@net.net"

PID=`/usr/ucb/ps -auxwww |grep -i [s]erver1 |grep -v grep | awk '{print $2}'`
if [ $PID = ] ; then
echo " WAS6 is down,please restart"
else
echo " WAS6 is running, no action needed"
fi
# ./test.ksh
./test.ksh[11]: test: argument expected
WAS6 is running, no action needed
#

~

if [ -z "$PID" ] ; then 
echo " WAS6 is down,please restart"
else
echo " WAS6 is running, no action needed"
fi

z option is used to check whether variable is null

thank you so very much... I HAVE search countless books and searched the entire library on www.books24x7.com and safair.orielly.com and none of then could tell me this...

i'll be a regular on this forum for sure...tks again...i'll try this method out

btw, do any of you guys know where I can get a list of these kind of directives and what they do?

Im trying to get better at shell scripting....but its like trying to write a letter without knowing the dictionary....

-z
-eq
-$?

etc..etc...etc...

You can read the following document :KornShell 88 Manual Page
You will find ather links on the page KornShell Documentation

Jean-Pierre.

thank you,will read!

man ksh will give you all the test expressions.

how would i be able to just verbose the first line of this output... I have the ptree showing the parent/child processes... I want it to print just the parent..which seems to be the first line in the output

bash-3.00# ptree |grep [h]ttpd |grep -v grep |awk '{print $1}'
21679
21680
21681
21682
bash-3.00#

ptree |grep [h]ttpd |grep -v grep |awk '{print $1}' | sed q

or

ptree |grep [h]ttpd |grep -v grep |awk '{print $1}' | head -1

Which grep are you using ? See if you have the -m flag.

Try this.

ptree |grep -m 1 "[h]ttpd" |awk '{print $1}' 

The second "grep -v grep" is a wasted call. Why ? Because, grep "[h]ttpd" does the same thing as grep httpd | grep -v grep

I tried that...did seem to like that one

bash-3.00# ptree |grep -m 1 "[h]ttpd" |awk '{print $1}'
grep: illegal option -- m
Usage: grep -hblcnsviw pattern file . . .
bash-3.00# pwd
/
bash-3.00# $PATH
bash: /usr/sbin:/usr/bin:/usr/local/: No such file or director

Thanks a bunch Glen....just what the doc ordered :wink:

bash-3.00# ptree |grep [h]ttpd |grep -v grep |awk '{print $1}'
21679
21680
21681
21682
bash-3.00# ptree |grep [h]ttpd |grep -v grep |awk '{print $1}' | head -1
21679
bash-3.00#

ok, so this is how far i've gotten so far....the trying to get the script to check if the PID of the httpd process exists...if it does not..send a email stating so. If the HTTPD process does exist, I want it to continue further and do URL state check, if the URL is not responding. I want the script the kill the process and restart it. So far this is what I got...errors..ha

bash-3.00# cat url_check.ksh
#!/bin/ksh
#
# Check to see if URLs respond in a given time period
#
sleeptime="30"
timeout="15"
BASE_DIR="/local/apps/adminutils"
WGET="$BASE_DIR/bin/wget"
DAT_FILE="$BASE_DIR/etc/url_check.dat"
MAIL_LIST="testuser1@testnet.com"
MAIL_LIST2="testuser2@testnet.com"
DEPLOYSTART="/local/apps/WebSphere6/IHS/0/w6u/ihst.sh ws1 start"

#----------------------------------------------------------------------------------------------------------------------------------------------------
# Check for support script
if [ ! -x $WGET ]; then
echo "ERROR Unable to execute $WGET"
fi
#----------------------------------------------------------------------------------------------------------------------------------------------------

#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Below checks for the DeployDirector PID, if it exits it continues to the website status check. dIf not, then it will send an email alert to the MAIL_LIST2 support group stating the daemon is down
#PID=`/usr/ucb/ps -auxwww |grep -i [h]ttpd |grep -v grep | awk '{print $1}'`
PID=`ptree |grep [h]ttpd |grep -v grep |awk '{print $1}' | head -1` > /dev/null
if [ -z "$PID" ] ; then
echo "DeployDirector Server Process PID Is Down." | mailx -s "DeployDirector Server Daemon is DOWN, this may be due to scheduled maintenence by the SA Support group. Confirmation is needed." "$MAIL_LIST2"
else

for url in `cat $DAT_FILE | grep -v "^#"`
do
$WGET -T $timeout -t 1 -q -O /dev/null ${url}
if [ $? -gt 0 ] ; then
/bin/sleep $sleeptime
$WGET -T $timeout -t 1 -q -O /dev/null ${url}
if [ $? -gt 0 ] ; then
# it's still down, still cannot access the URL - we've got a problem!!

                            kill -9 $PID && $DEPLOYSTART

    
                            echo "$\{url\} cannot be accessed. \(2 attempts, $\{sleeptime\} seconds between each attempt, $\{timeout\} second timeout\)" | mailx -s "Possible Website Issue with $\{url\}" "$MAIL_LIST"

# Uncomment below to log to syslog
#/bin/logger -p user.err -t WEB-ALERT "Website issue: <a href=\"${url}\">${url}</a> cannot be accessed after ${sleeptime} seconds, with a $timeout second timeout.
fi
fi
done
bash-3.00# ./url_check.ksh
./url_check.ksh[27]: syntax error at line 29 : `else' unmatched
bash-3.00#

'man ksh' yields the following for the 'if' construct:


     if list ; then list ; [ elif list ; then list ; ... ] [ else list ; ] fi
               The  list  following  if   is  executed and, if it
               returns an exit status of 0,  the  list  following
               the  first  then is executed.  Otherwise, the list
               following elif is executed and, if its value is 0,
               the  list  following  the  next  then is executed.
               Failing that, the else list  is  executed.  If  no
               else  list  or  then list is executed, then the if
               command returns 0 exit status.

'vBcodes' can be found here

Please use CODE tags; it's difficult to read your code otherwise. Looks like you need a "fi" after the "done".