How to avoid hanging wget in bash

I have a .sh script, which, among other things, includes exchange rates from Google as follows:

printf 'Bash: Going to get exchange rates'
echo    
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=aud" |  sed '/res/!d;s/<[^>]*>//g' > exrates
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=jpy" |  sed '/res/!d;s/<[^>]*>//g' >> exrates
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=hkd" |  sed '/res/!d;s/<[^>]*>//g' >> exrates
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=nzd" |  sed '/res/!d;s/<[^>]*>//g' >> exrates
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=eur" |  sed '/res/!d;s/<[^>]*>//g' >> exrates
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=gbp" |  sed '/res/!d;s/<[^>]*>//g' >> exrates

mv /home/stan/perl/2014/scripts/exrates /home/stan/perl/2014/exrates/exrates

printf 'Bash: Got exchange rates'
echo

Sometimes the script hangs here. I do not mind not updating these bets every time it works, if it freezes, I would generally skip this step, but how?

What should I include in the if statement to check if wget can receive data quickly or will it last forever? A little more detail on wget will not hurt either.

By the way, I don’t know why the vigette is hanging. The browser opens these pages in order, and the same commands are launched from the command line.

+4
source share
3 answers

, , HTTP-, script. , , , IP-.

sleep . :

getExchangeRates() {
  wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=$1" |  sed '/res/!d;s/<[^>]*>//g' >> exrates
  sleep 10   # Adding a 10 second sleep
}

, :

getExchangeRates aud

:

for currency in aud jpy hkd nzd eur gpb; do
  getExchangeRates $currency
done
+4

- wget

wget --timeout 10 <URL>

   wget

+3

wget has various timeout options. On the man page

   --timeout=seconds
       Set the network timeout to seconds seconds.  This is equivalent to
       specifying --dns-timeout, --connect-timeout, and --read-timeout,
       all at the same time.

So you can just set --timeout, or if you think this is one of the other factors, you can set a specific timeout

+2
source

Source: https://habr.com/ru/post/1537464/


All Articles