Sponsored Content
Top Forums Shell Programming and Scripting Command timeout from inside script. Post 302594892 by gencon on Wednesday 1st of February 2012 11:58:22 AM
Old 02-01-2012
Command timeout from inside script.

Hi,

I've written a very robust script to get an external IP address from 'behind' a router. It uses many web pages randomly choosing which one/ones to use at run time. The "fetch the web page containing the IP address" is handled by either wget or curl both of which have their 'max time for the operation' option set to 1 second. Most of the web servers respond within a max of 500ms, so the idea of having the timeout set to 1 second is so that if a server is slow to respond the script just moves on to another server.

I've noticed that neither curl nor wget actually keep to the timeout. On my machine this was not a problem, the timeout always kicked in at the latest of about 2.5 seconds. Over the last 24 hours I've tested the script on a Debian machine, having the script run 10 times every 5 mins. Looking at the results I've discovered that on 14 occasions (of the 7560) the timeout failed catastrophically, ranging from 27.722 to 170.643 seconds - the latter is almost 3 mins from a 1 sec timeout.

It seems that I'll have to write my own timeout routine. There are quite a lot of examples out there but the all seem to rely on either using the program 'timeout' (which is not POSIX), or using a timeout script, Eg. timeout_script -t 1 command - which is not what I need at all.

Here's a little bit of my code:

Code:
# curl command line for curl users.
curlCommand="curl --silent --max-time $timeout"

# wget command line for wget users.
wgetCommand="wget --quiet --timeout=$timeout --tries=1 --output-document=-"

urlDownloaderProg=$curlCommand

grepExpr="[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"

ipAdd=$($urlDownloaderProg "$url" | grep -Eo "$grepExpr" | uniq)

What I need to do is for the line beginning 'ipAdd=' to return immediately, and then for the script to monitor its child process and kill it if it is not finished after 1 second. But I'm not sure how to do this - in particular how do I get the ipAdd= line to return immediately, I had problems using '&' both inside and outside the final ')', while still storing the results of the line in the variable ipAdd. Maybe I am going about this the wrong way and need a different approach.

Any help or advise would be greatly appreciated. Many thanks.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Run the command inside perl script

I have a command which will run fine in a unix command prompt. Can you tell how to interprete this command inside perl script...... The command is : perl -pe 's/(\|333\}.*)\}$/$1|1.6}/' FIA.txt This will search for the number 333 and appends 1.6 at the end of that line....... (1 Reply)
Discussion started by: vinay123
1 Replies

2. UNIX for Dummies Questions & Answers

Exit out of the Script Command inside a Script

I'm new to Linux. I have a bash script that invokes an executable. I'd like use the SCRIPT command inside the script and exit out of the script command after it writes to the file. Does this make sense? Below is an example of the contents of my script. #BEGIN SCRIPT script typescript... (6 Replies)
Discussion started by: jmungai
6 Replies

3. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

4. Shell Programming and Scripting

How to monitor a command inside shell script

Hi All, Is there any way to monitor a command inside shell script ? I have a script inside which I have a tar command which zips around 200GB data. tar zcvf $Bckp_Dir/$Box-BaseBackup-$Day.tar.gz * --exclude 'dbserver_logs/*' --exclude postmaster.pid --exclude 'pg_xlog/*' I want to... (3 Replies)
Discussion started by: sussus2326
3 Replies

5. Shell Programming and Scripting

Using ssh command inside a script

Hi, I have a script file in server A. Inside the script file, I first have a ssh command that will connect to a remote server B. In the same script file itself, I have a sequence of commands that has to be run in server B. I am embedding these commands in the script file that I have in server A.... (2 Replies)
Discussion started by: mick_000
2 Replies

6. Shell Programming and Scripting

Can i use if else inside expect command in shell script?

hii,, I am trying to automate jira. during my scripting using bash script, in the terminal i got the terminal message like this: "Configure which ports JIRA will use. JIRA requires two TCP ports that are not being used by any other applications on this machine. The HTTP port is where you... (1 Reply)
Discussion started by: nithinfluent
1 Replies

7. Shell Programming and Scripting

Issue with ls command inside script

Hi , DIR1 has only one file with .txt extension , trying to get the size of that file using the following script #!/bin/ksh foldr_1="/etc/DIR1" #echo "$foldr_1" sze_fdr1=$(ls -ltr foldr_1/*.txt |awk '{ print $5 }') echo "$sze_fdr1" After executing the above script getting... (1 Reply)
Discussion started by: smile689
1 Replies

8. Shell Programming and Scripting

How to find whether a particular command has failed inside an sftp script?

hi, how can i know whether a command inside an sftp script has failed or not? i have a sftp expect script #!/usr/bin/expect spawn /usr/bin/sftp abc@ftp.abc.com expect "sftp>" send "cd dir\r" expect "sftp>" send "mput abc.txt\r" expect "sftp>" send "mput def.xls\r" expect "sftp>"... (5 Replies)
Discussion started by: Little
5 Replies

9. Shell Programming and Scripting

Using script command inside a script or emulating it

My script needs to take a folder path to the location of a C program, and run the program until the user selects exit in his C program. The script needs to create a transcript to a text file while the C program is running. Creating a transcript can be easily done with the script command but it... (2 Replies)
Discussion started by: syntax_eror
2 Replies
curl_multi_timeout(3)						  libcurl Manual					     curl_multi_timeout(3)

NAME
curl_multi_timeout - how long to wait for action before proceeding SYNOPSIS
#include <curl/curl.h> CURLMcode curl_multi_timeout(CURLM *multi_handle, long *timeout); DESCRIPTION
An application using the libcurl multi interface should call curl_multi_timeout(3) to figure out how long it should wait for socket actions - at most - before proceeding. Proceeding means either doing the socket-style timeout action: call the curl_multi_socket_action(3) function with the sockfd argument set to CURL_SOCKET_TIMEOUT, or call curl_multi_perform(3) if you're using the simpler and older multi interface approach. The timeout value returned in the long timeout points to, is in number of milliseconds at this very moment. If 0, it means you should pro- ceed immediately without waiting for anything. If it returns -1, there's no timeout at all set. An application that uses the multi_socket API SHOULD not use this function, but SHOULD instead use curl_multi_setopt(3) and its CURL- MOPT_TIMERFUNCTION option for proper and desired behavior. Note: if libcurl returns a -1 timeout here, it just means that libcurl currently has no stored timeout value. You must not wait too long (more than a few seconds perhaps) before you call curl_multi_perform() again. RETURN VALUE
The standard CURLMcode for multi interface error codes. TYPICAL USAGE
Call curl_multi_timeout(3), then wait for action on the sockets. You figure out which sockets to wait for by calling curl_multi_fdset(3) or by a previous call to curl_multi_socket(3). AVAILABILITY
This function was added in libcurl 7.15.4. SEE ALSO
curl_multi_fdset(3), curl_multi_info_read(3), curl_multi_socket(3), curl_multi_setopt(3) libcurl 7.16.0 2 Jan 2006 curl_multi_timeout(3)
All times are GMT -4. The time now is 11:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy