Re: if statement error handling (246 Views)
Reply
Super Advisor
himacs
Posts: 611
Registered: ‎01-27-2009
Message 1 of 6 (257 Views)

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

 

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 6 (246 Views)

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...

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,774
Registered: ‎03-06-2006
Message 3 of 6 (239 Views)

Re: if statement error handling

[ Edited ]

>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}
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 6 (233 Views)

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-gre...

 

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...

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,774
Registered: ‎03-06-2006
Message 5 of 6 (232 Views)

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. ;-)

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 6 (228 Views)

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...

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation