Operating System - HP-UX
1834178 Members
2161 Online
110064 Solutions
New Discussion

Re: how to set timeout in shell script

 
Ridzuan Zakaria
Frequent Advisor

how to set timeout in shell script

Hi,

I am writing a script to connect to databases on remote servers. Something the script hang will attemp to connect the the databases. I would like to set timeout if attemp to connect to the database exceed 10 secs and move to the next one.

i.e:

while read host
do
connect_db
done < hosts.lst

Thanks.
quest for perfections
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: how to set timeout in shell script

I don't know how to do that in the shell script.

What is connect_db? Can the functionality be built into that?

I'll give you an example of how it works.

ping has a timeout command line option in it.

ping -m 10 target

That says try the ping for 10 seconds and then give up.

I'll be watching this thread to learn.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ridzuan Zakaria
Frequent Advisor

Re: how to set timeout in shell script

Hi, the connect_db function is actually run the following command to check Oracle listener trace level on the remote servers.

However on some servers the following command run forever:

lsnrctl show trc_level

I would like to skip any servers that take longer than 10 sec to run the above command.

Thanks.
quest for perfections
Sundar_7
Honored Contributor

Re: how to set timeout in shell script

Start connect_db in the background and run the checks in the script

while read host
do
connect_db &
PID=$!
sleep 10
kill -0 $PID >/dev/null 2>&1
[ $? -eq 0 ] && kill $PID
done
Learn What to do ,How to do and more importantly When to do ?
Abdul Rahiman
Esteemed Contributor

Re: how to set timeout in shell script

I don't think there is an easy way to do this the you want.. or may be there is some still undiscovered sheel options somewhere to get what you wanted.
But here is few things that came to my mind to tackle the rpoblem.

1. You may probably want to find out why the 'lsnrctl' command is hangign in the first place. IT may be related to some buggy code or require a patch or something.

2. Or may be it's related to some poor netork (tcp/ip) settings. In that case you may be able to tune some ndd paramteres like
tcp_ip_abort_cinterval etc. (look at ndd -h to see all supported ndd tunable paramteres).
This might help set the timeout value to what you are looking for.

3. In a shell scripting perspective, I would probabley run the command in the background and do sleep for 10 seconds and would kill it with kill -9 if still hanging. Just a workaround.
or as a last resort,
4. Continue looking for a ksh timeout value ..:-

reds,
Abdul.
No unix, no fun