如何避免在 bash 中挂起 wget
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23213087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid hanging wget in bash
提问by Stangoesagain
I have .sh script that, among other things, wgets currency exchange rates from Google as follows:
我有 .sh 脚本,除其他外,从 Google 获取货币汇率如下:
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
Occasionally script hangs here, however. I don't mind not updating these rates every time it runs, if it hangs I'd like to skip this step altogether, but how?
然而,有时脚本会挂在这里。我不介意每次运行时不更新这些费率,如果它挂起我想完全跳过这一步,但是如何?
What should I put in "if" statement to check if wget can fetch data promptly or will take forever? A little more verbosity in wget execution wouldn't hurt either.
我应该在“if”语句中放入什么来检查 wget 是否可以及时获取数据或将永远获取数据?在 wget 执行中多一点冗长也不会受到伤害。
Btw, I don't know why wget hangs. Browser opens those pages okay, and same commands run from terminal line by line work, too.
顺便说一句,我不知道为什么 wget 挂起。浏览器可以正常打开这些页面,并且同样的命令也可以从终端逐行运行。
采纳答案by devnull
I assume that it hangs because you have a number of HTTP requests being sent to a single host in a script. The host in question doesn't like that too much and it starts to block requests from your IP address.
我假设它挂起是因为您有许多 HTTP 请求被发送到脚本中的单个主机。有问题的主机不太喜欢这样,它开始阻止来自您 IP 地址的请求。
A simple workaround would be to put a sleep
in between the requests. You could also make use of a function:
一个简单的解决方法是sleep
在请求之间放置一个。您还可以使用一个函数:
getExchangeRates() {
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=" | sed '/res/!d;s/<[^>]*>//g' >> exrates
sleep 10 # Adding a 10 second sleep
}
and invoke it by passing a parameter to the function:
并通过向函数传递参数来调用它:
getExchangeRates aud
The function could also be invoked in a loop for various currencies:
该函数也可以在各种货币的循环中调用:
for currency in aud jpy hkd nzd eur gpb; do
getExchangeRates $currency
done
回答by PradyJord
use timeout in wget statement only
仅在 wget 语句中使用超时
wget --timeout 10 <URL>
timeout is in seconds and put some sleep in between two wgets
超时以秒为单位,并在两个 wget 之间放置一些睡眠
回答by Vorsprung
wget has various timeout options. From the man page
wget 有各种超时选项。从手册页
--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 simply set --timeout, or if you believe it's one of the other factors alone you can set a specific timeout
所以你可以简单地设置 --timeout,或者如果你认为这是其他因素之一,你可以设置一个特定的超时