- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: if statement error handling
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2011 03:05 AM
10-18-2011 03:05 AM
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
- Tags:
- PS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2011 05:12 AM
10-18-2011 05:12 AM
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...
- Tags:
- UNIX95
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2011 09:48 AM - edited 10-18-2011 09:53 AM
10-18-2011 09:48 AM - edited 10-18-2011 09:53 AM
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}
- Tags:
- EXTENDED_PS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2011 10:05 AM
10-18-2011 10:05 AM
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:
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2011 10:13 AM
10-18-2011 10:13 AM
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. ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2011 10:30 AM
10-18-2011 10:30 AM
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...