How to skip command if it is hanging while waiting for response


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to skip command if it is hanging while waiting for response
# 1  
Old 02-24-2011
How to skip command if it is hanging while waiting for response

Hello,

I have a script that contains the command "whois 1.2.3.4"

Sometimes this command takes far too long to produce any output and as a result the rest of the script is not executed.

Can anyone suggest a method so that if no output is produced after say 2 seconds the script skips that command and continues on. I imagine some sort of if statement would do this.

Any assistance would be greatly appreciated.

Regards,
Colin
# 2  
Old 02-24-2011
Do you have expect installed?
If not, you could run the command in background, save its pid, sleep for 2 seconds and, if the program is still running, kill it.
# 3  
Old 02-24-2011
How about tis (if your sleep dosn't support decimal values ie only whole integers you will need to sleep 1 instead and reduce your wait count to 2).
If you don't have the /proc filesystem you will need to run ps -p $WPID 2> /dev/null and test the exit status

Code:
#!/bin/bash
whois 1.2.3.4 > /tmp/wi$$.out &
WPID=$!
while [[ -d /proc/$WPID ]] && [[ $((++wait)) -lt 8 ]]
do
    sleep 0.25
done
if [[ -d /proc/$WPID ]]
then
    WHOIS="<unknown>"
    echo "whois taking too long - killing"
    kill $WPID
else
    WHOIS=$(cat /tmp/wi$$.out)
fi
rm /tmp/wi$$.out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk hanging from running command

awk -v youth=1599 -v d="$(/bin/ps -ef)" 'd ~/ 1599 / && d !~/ awk / && (($2 == youth) || ($3 == youth)) {print $2" "$3}' i feel like i'm very close here. what am i doing wrong? (1 Reply)
Discussion started by: SkySmart
1 Replies

2. Shell Programming and Scripting

sed Command to skip

Friends, I have 1000 line like below #formate Test.server.e01=http://111.22.33.23/ #test_server_west Test.server.w01=http://112.123.123.22/ Test.server.w02=http://113.143.123.22/ Test.server.w03=http://112.183.123.22/ #test_server_east Test.server.e01=http://115.123.123.22/... (6 Replies)
Discussion started by: jothi basu
6 Replies

3. Shell Programming and Scripting

Can I skip files when running rm command

Platform: Oracle Enterprise Linux 6.2 I have several files like below. I want to remove all files except one file For example , I want to remove all the files below except dasd_91197.trc $ ls -alrt *.trc -rw-r----- 1 ecmdev wms 8438784 May 7 21:30 dasd_91177.trc -rw-r----- 1 ecmdev wms ... (3 Replies)
Discussion started by: John K
3 Replies

4. UNIX for Dummies Questions & Answers

passing input into a cmd line that's waiting for a response

Hi, I am writing a little script to update a parameters on JMQ. however the JMQ requires a "y" confirmation to be input as part of the cmd I am running. However I want run this script to offline with no input from a user. it works if a I create a file with with just y in it and pass that in... (3 Replies)
Discussion started by: shropshirehobbi
3 Replies

5. Solaris

df command hanging

Hi Folks, When i execute the command df -kh in my system the o/p hangs.. The command runs fine but takes a lot of time before coming back to the # prompt. Can anyone please suggest the possible cause and solution?. (10 Replies)
Discussion started by: vivek.goel.piet
10 Replies

6. Shell Programming and Scripting

read command not waiting

Dear All, read command not waiting for my input, please suggest. code: while read line do echo "$line " | grep -i .par$ if ; then cd ../par echo " Do you want to proceed" read else cd ../sql read fi done <inp.txt Its not asking me any input? (2 Replies)
Discussion started by: mahendra singh
2 Replies

7. UNIX for Dummies Questions & Answers

Sz command hanging and outputting ZNAK’s

Currently I am using the following sz and rz commands to run file transfer using ZMODEM within a perl script. sz -b -m1 -M2 -k -e -r -u -t10 rz -b -m 1 -M 120 -y Is there any flag setting that will allow me to terminate when a retry 0 error is outputted as this is constantly filling up my... (0 Replies)
Discussion started by: M1keSt4r
0 Replies

8. UNIX and Linux Applications

waiting for ssh response for seconds

Hi All, I have to make an alert that'll wait for ssh response from the server for certain seconds, if no response is there in between it'll raise an alarm. Havn't found any option for this yet, pls. help if anyone knows abt this. Any suggestion is welcome. :) Best Regards, VG (3 Replies)
Discussion started by: vguleria
3 Replies

9. UNIX for Dummies Questions & Answers

ps command is hanging

Hi All, I am unable to figure out why my ps command is haning. Is some one else is running a process which is hanged. But in that case also if i do ps it should show only the processes running by me only. Thanks & Regards Gauri Agrawal (2 Replies)
Discussion started by: gauri
2 Replies

10. Shell Programming and Scripting

read-waiting for user response

Would there be any reason for why a 'read ans' would not wait for a user's response (i.e user has to hit a key to continue)? I know for sure that it is doing everything else in that part of my 'if' statement but it doesn't wait for me to hit a key before continuing. The strange thing is that... (4 Replies)
Discussion started by: giannicello
4 Replies
Login or Register to Ask a Question