wget question

Status
Not open for further replies.

TheDan666

Ars Tribunus Militum
2,359
I have a shell script that runs a daily job and uses wget to kick off a URL request as part of that job. My question is, given the following output, does wget consider the HTTP request successful? If it does not, would putting in -tries=10 force wget to keep trying 10 times until it gets a 200 HTTP response code? In general, the HTTP request works fine but I have seen instances like this before sporadically:

Code:
--18:49:42--  http://www.mysite.com/xxx.cfm
           => `index.html'
Resolving www.mysite.com... 255.255.255.255
Connecting to www.mysite.com|255.255.255.255|:80... connected.
HTTP request sent, awaiting response... 502 Proxy Error
18:54:42 ERROR 502: Proxy Error.

If wget does consider this successful, is the only option to grep the outputed index.html and look for a 200 response code and I don't see it keep trying via a shell script while loop?

Thanks,
The Dan
 

TheDan666

Ars Tribunus Militum
2,359
andrewme":rny0ht0z said:
I would be very surprised if wget didn't consider the 502 a failure. To check wget's behavior when the real failure is intermittent, you could try using the --tries option with a bogus URL that would definitely 404 and make sure it retries.

I would think so as well. It's just that I'd like to know for sure because this is a a rarely occurring condition that I wouldn't be able to recreate in testing.

--The Dan
 

TheDan666

Ars Tribunus Militum
2,359
TheDan666":20avtpga said:
from the wget man page. Don't like the sounds of this.

--tries=number
Set number of retries to number. Specify 0 or inf for infinite retrying. The default is to retry 20 times, with the exception of fatal errors like ''connection refused'' or ''not found'' (404), which are not retried.

But, hark, look at this:

--retry-connrefused
Consider ''connection refused'' a transient error and try again. Normally Wget gives up on a URL when it is unable to connect to the site because failure to connect is taken as a sign that the server is not running at all and that retries would not help. This option is for mirroring unreliable sites whose servers tend to disappear for short periods of time.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
Simply check $?, it is probably non-zero. A 404 gets an 8, an invalid cert gets a 5, and an actual page gets a 0. As is normal for unix commands.
Code:
wget $FLAGS $URL > $OUTFILE
RESULT=$?
if [ "$RESULT" != "0" ]
then
  echo "Received an error of $RESULT, aborting."
  exit
fi
Technically you should check to see if $RESULT has a length, but if assigning $? to RESULT fails you have other problems :)
 

TheDan666

Ars Tribunus Militum
2,359
ronelson":vevur0cs said:
Simply check $?, it is probably non-zero. A 404 gets an 8, an invalid cert gets a 5, and an actual page gets a 0. As is normal for unix commands.
Code:
wget $FLAGS $URL > $OUTFILE
RESULT=$?
if [ "$RESULT" != "0" ]
then
  echo "Received an error of $RESULT, aborting."
  exit
fi
Technically you should check to see if $RESULT has a length, but if assigning $? to RESULT fails you have other problems :)

Thanks! That seems much more robust. I think I will use this in conjunction with a while loop, an iterator, and a pause of say 1 minute to make sure the thing tries to execute X number of times.

--The Dan
 

TheDan666

Ars Tribunus Militum
2,359
Thanks for the help everyone. This was what I ended up going with:

Code:
#local variables to determine whether to proceed with while loop
v_result=-1
v_tries=1
v_max_tries=10

while [ "$v_result" != "0" ] && [ "$v_tries" -lte "$v_max_tries" ]

do

	#if this is not the first time through, pause for a minute to give the server more time to resolve itself
	if [ "$v_tries" != "1" ]; then
		sleep 60
	fi
	
	wget $v_url -t 4000 -o output.txt -O index.html
	v_result=$?		#store result of wget operation
	v_tries=$(( $v_tries + 1))
	
done

--The Dan
 

bdr

Wise, Aged Ars Veteran
137
It pains me to see the while-construct abused in this way. You almost never need to check the "$?" variable directly because that's exactly what while and if are made for.

A better way to write this script would be
Code:
#! /bin/sh

url="http://www.example.com"
logfile="output.txt"
output="index.html"
max_tries=10
tries=1

while ! wget "$url" -t 4000 -o "$logfile" -O "$output"
do
    if [ "$tries" -gte "$max_tries" ]
    then
        break
    fi

    tries=`expr "$tries" + 1`
    sleep 60
done
 

TheDan666

Ars Tribunus Militum
2,359
bdr":1vphzuer said:
It pains me to see the while-construct abused in this way. You almost never need to check the "$?" variable directly because that's exactly what while and if are made for.

A better way to write this script would be
Code:
#! /bin/sh

url="http://www.example.com"
logfile="output.txt"
output="index.html"
max_tries=10
tries=1

while ! wget "$url" -t 4000 -o "$logfile" -O "$output"
do
    if [ "$tries" -gte "$max_tries" ]
    then
        break
    fi

    tries=`expr "$tries" + 1`
    sleep 60
done


I see where you are coming from and I am essentially bastardizing the while loop to some degree. Looking at your example, though, it doesn't seem quite as intuitive as to what the code is doing. Perhaps this is because I wrote my example so I'm obviously biased. The line of:

while ! wget "$url" -t 4000 -o "$logfile" -O "$output"

strikes me as somewhat weird because wget can return any number of things. Does the code consider a result of 0 to be equivalent to true and anything else as being false?

--The Dan
 

bdr

Wise, Aged Ars Veteran
137
while considers everything with a zero exit status as true and everything else as false, yes. That's why I used the exclamation mark, which is a negation (NOT). For me, "while wget doesn't succeed" reads a lot easier than "while some value is different from zero".

What may not be clear to you, is the fact that while and if in shellscripting know about nothing else than exit statusses. When you write while [ "$result" != "0" ], what actually happens is*:

  • the shell expands $result to it's current value, let's say 6.
  • the test program also known as [ is invoked with parameters '6', '!=', '0' and ']'
  • the test program returns it's evaluation of the logical test given as exit status, meaning 0 is TRUE and 1 (or everything else) is FALSE.
  • based on test's exit status, while enters or skips the inner loop.

If you want to know more about exit-statusses and shell scripting, experiment with the programs true, false, [ and test. You can see where they reside with which (f.i. which [ will show you were [ resides, in /bin/[ on my Mac OS X system).



*recent shells may have the test program as a builtin, meaning to no extra process will be started, but the semantics stay the same.
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
That is the way the while loop works. A '0' is true and non-zero is false. It is the same with $? or VAR=$?, you are just short-cutting the assignment to another variable. I used assignment because it makes for an easy example, but you do not need it unless you plan to use the result later (which I made sure my example did) because $? is updated with the result of every command:
Code:
# grep blahblahblacksheep /etc/hosts
# echo $?
1
# echo $?
0
If you really want it to be readable without the awkward while construct, try something like this (caution: untested code):
Code:
logfile="output.txt"
output="index.html"

check_url() {
   URL=$1
   wget $URL -t 4000 -o $logfile -O $output
   return $?
}

url="http://www.example.com"
max_tries=10
wait_time=60
tries=1

while ! check_url $url
do
    if [ "$tries" -gte "$max_tries" ]
    then
        break
    fi

    tries=`expr "$tries" + 1`
    sleep $wait_time
done

If you want to know more about exit-statusses and shell scripting, experiment with the programs true, false, [ and test. You can see where they reside with which (f.i. which [ will show you were [ resides, in /bin/[ on my Mac OS X system).
Also, buy a book on shell scripting. It is painful to try and google for things like $? or $* :)
 

bdr

Wise, Aged Ars Veteran
137
ronelson":2qd3e93x said:
Code:
check_url() {
   URL=$1
   wget $URL -t 4000 -o $logfile -O $output
   return $?
}

This, again, is unnecessary. The return value of a shell function is the exit status of the last command in the function, so this would suffice:

Code:
check_url() {
   wget "$1" -t 4000 -o "$logfile" -O "$output"
}

And BTW, don't forget to quote your variables. Your function will not work with either an URL with spaces, a logfile-name with spaces, or an output-name with spaces or all of them together. (As a matter of fact, any variable in a shell-script should be quoted, unless you're absolutely sure it will never contain spaces (like counters). Then again, it doesn't hurt to quote even counters.)
 

ronelson

Ars Legatus Legionis
21,399
Subscriptor
This, again, is unnecessary. The return value of a shell function is the exit status of the last command in the function, so this would suffice:
Same thing in other languages, but I prefer to be explicit. Helps three years later when you review it. This is also why I have "URL=$1" when it is not necessary. It is not like we are talking about a huge performance impact here in a 20 line program :)

For vars, I prefer rigorous error checking and alerts to quoting, but that is me. I like to know that $logfile was empty rather than wonder what missing option to wget was the problem. Definitely important to understand though.
 

TheDan666

Ars Tribunus Militum
2,359
Thanks for all of the insight. I'm not really well-versed in shell scripting. I've used it before but this is really the first time I've had to do anything relatively important with it. I'm appreciating all of its power but I still find it pretty finicky (can't have spaces between assignments, etc) and very much "dark arts" at the moment :)

--The Dan
 

bdr

Wise, Aged Ars Veteran
137
The important thing to remember is that the shell is way more stupid than you think. Strange syntactical constructs stem from this stupidity.

In short, this is what the shell does:
  1. Read a line.
  2. Subsitute any variables by their current value ($HOME becomes /home/bdr/).
  3. Split the line into different items delimited by one or more characters mentioned in the special $IFS variable (internal field separator, 99% of the time equal to spaces and tabs.).
  4. Split all items by '=' and define the left hand part as a variable with the value of the right hand part. Take them out of the list of items.
  5. Take out any shell keywords (while, if, let, set, &&, ||, etc.) and take appriopriate action for them.
  6. Execute the first item (program) with the remainder of items as parameters.

The reason you can't have spaces in variable assignment is that the shell regards "a = value" as a command and will execute program "a" with 2 parameters: "=" and "value". In other words, step 3 trumps step 4.

The reason you really should quote all variables is that step 2 (variable expansion) preceeds step 3 (split by whitespace). A quick example

Code:
var="&& rm -rf /"
echo "Var value" $var

will be executed as

Code:
echo "Var value" && rm -rf /

and you do not ever want to run that as root.
 
Status
Not open for further replies.