Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

if statement error handling

himacs
Super Advisor

if statement error handling

count=`ps -ef | grep LIST_8i | grep -v grep|wc -l`
pid=`ps -ef|grep LIST_8i|grep -v grep|awk '{print $2}'`
if [ $count = 1 ]
then
lsnrctl stop LIST_8i >> ${log}
sleep 5
lsnrctl start LIST_8i >> ${log}
else
echo " "
lsnrctl start LIST_8i >> ${log}
fi 2>&1


The script  restarts listener if count is equal to 1.

This script fails when 'lsnrctl stop LIST_8i ' code aborts due to some tns adapter error.
In this scenario i had to use kill -9 <pid>.


Now i want modify script like when 'lsnrctl stop LIST_8i ' fails and kill -9 <pid> works.

Please guide me how i can achieve this.

Regards
himacs

 

5 REPLIES
Highlighted
James R. Ferguson
Acclaimed Contributor

Re: if statement error handling

Hi:

 

First, avoid matching what you don't want to match by using the UNIX95 (XPG4) 'ps' behavior.  You can redirect both STDOUT and STDERR to your ${log} once with an 'exec'.

 

# cat ./dolistener
#!/bin/sh
MYPROC=LIST_8i
PID=$(UNIX95= ps -C -o pid= ${MYPROC})
exec 2>&1 >> ${log} #...redirect STDOUT & STDERR to ${log}
if [ -z ${PID} ]; then #...nothing found...
    lsnrctl start ${MYPROC}
    exit 0
fi
lsnrctl stop ${MYPROC}
if [ $? ] -ne 0; then
    kill -9 ${PID}
    exit 1
fi
sleep 5
lsnrctl start ${MYPROC}
exit $?

 The use of UNIX95 is confined to the 'ps' command.  This is done by writing 'UNIX95=' followed immediately by whitespace; followed by the command whose behavior we want to alter.  The '-C' option of 'ps' matches a process's basename.  You may need to change the MYPROC value to match.

 

The '-o pid=' says to return the PID of any process(es) found.  The '=' notation says to suppress the header line that would otherwise appear.

 

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: if statement error handling

>avoid matching what you don't want to match by using the UNIX95 (XPG4) 'ps' behavior.

 

You have a few typos and improvements that you can go back and edit:

>PID=$(UNIX95= ps -C -o pid= ${MYPROC})

 

PID=$(UNIX95=EXTENDED_PS ps -C ${MYPROC} -opid=)

 

>if [ $? ] -ne 0; then

 

if [ $? -ne 0 ]; then

 

>You can redirect both STDOUT and STDERR to your ${log} once with an 'exec'.

 

I think your order should be:

exec >> ${log} 2>&1 #... redirect STDOUT & STDERR to ${log}
James R. Ferguson
Acclaimed Contributor

Re: if statement error handling

Hi (again):

 

> Dennis: You have a few typos and improvements that you can go back and edit:

 

So you play golf? :-)  Yes, I could shorten any of the '-o' +name toggles to drop the space.

 

You are correct, of course on the test for the return value and on the 'exec'.  Aside from a quick syntax check (which didn't detect the wrongly written test, the code was "ok".

 

As for the UNIX95= versus UNIX95=EXTENDED_PS or UNIX95=whatever the reader should see our exchanges in this thread:

 

http://h30499.www3.hp.com/t5/System-Administration/ps-ef-appname-grep-v-grep-How-to-avoid-grep-v-grep-quot/m-p/4282537#M335670

 

I agree that your EXTENDED_PS highlights the intention, though if a semicolon is inadvertantly added, then the setting persists for more than the intended command line.

 

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: if statement error handling

>Aside from a quick syntax check (which didn't detect the wrongly written test, the code was "ok".

 

You failed the ps(1) semantic check.  The "-C" and its parm were separated by the -o.

The other changes were the improvements. ;-)

James R. Ferguson
Acclaimed Contributor

Re: if statement error handling


Dennis Handly wrote:

You failed the ps(1) semantic check.  The "-C" and its parm were separated by the -o.

The other changes were the improvements. ;-)


Oops!  Thanks.  ENOCOFFEE ...or not enough, clearly :-)

 

So for clarity, we have:

 

#!/bin/sh
MYPROC=LIST_8i
PID=$(UNIX95=EXTENDED_PS ps -C ${MYPROC} -opid=)
exec >> ${log} 2>&1 #...redirect STDOUT & STDERR to ${log}
if [ -z ${PID} ]; then #...nothing found...
    lsnrctl start ${MYPROC}
    exit 0
fi
lsnrctl stop ${MYPROC}
if [ $? -ne 0 ]; then
    kill -9 ${PID}
    exit 1
fi
sleep 5
lsnrctl start ${MYPROC}
exit $?

And in deference to you, I even used EXTENDED_PS ;-)

 

Regards!

 

...JRF...