bash wget 一起使用 --timeout 和 --trys
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43662591/
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
wget using --timeout and --tries together
提问by mickadoo
How long would you expect this command to take before exiting?
在退出之前,您预计此命令需要多长时间?
wget --timeout=1 --tries=2 "http://www.google.com:81/not-there"
wget --timeout=1 --tries=2 "http://www.google.com:81/not-there"
I would expect a timeout of 1 seconds and 2 tries would mean 2 seconds, but it takes 6.025 seconds
我希望超时 1 秒和 2 次尝试意味着 2 秒,但需要 6.025 秒
wget --timeout=1 --tries=2 "http://www.google.com:81/not-there"
--2017-04-27 16:49:12--http://www.google.com:81/not-there
Resolving www.google.com (www.google.com)... 209.85.203.105, 209.85.203.103, 209.85.203.99, ...
Connecting to www.google.com (www.google.com)|209.85.203.105|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.103|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.99|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.104|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.106|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.147|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|2a00:1450:4009:80d::2004|:81... failed: Network is unreachable.
I don't know why it tries 6 times before quitting.
我不知道为什么它在退出之前尝试了 6 次。
It seems like tries
is for retrying. If I set --retry-connrefused
it at least does the retry, but still the total time taken isn't what I would expect. I'd like to be able to decide how many times it should try on a timeout.
好像tries
是为了重试。如果我设置--retry-connrefused
它至少会重试,但总时间仍然不是我所期望的。我希望能够决定它应该尝试多少次超时。
Edit:
编辑:
After a suggestion from @Socowi I tried using waitretry in combination with retry-connrefused and got the same behaviour:
在@Socowi 提出建议后,我尝试将 waitretry 与 retry-connrefused 结合使用并得到相同的行为:
$ wget --timeout=1 --waitretry=0 --tries=2 --retry-connrefused "http://www.google.com:81/not-there"
--2017-04-27 20:29:47-- http://www.google.com:81/not-there
Resolving www.google.com (www.google.com)... 2a00:1450:400b:c00::68, 209.85.203.99, 209.85.203.147, ...
Connecting to www.google.com (www.google.com)|2a00:1450:400b:c00::68|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.99|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.147|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.103|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.104|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.106|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.105|:81... failed: Connection timed out.
Retrying.
--2017-04-27 20:29:54-- (try: 2) http://www.google.com:81/not-there
Connecting to www.google.com (www.google.com)|2a00:1450:400b:c00::68|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.99|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.147|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.103|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.104|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.106|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.105|:81... failed: Connection timed out.
Giving up.
Edit Two
编辑二
I was really confused when two identical commands behaved differently.
当两个相同的命令表现不同时,我真的很困惑。
root@8c59d6dd05fe:/var/www/html# wget --timeout=1 --waitretry=0 --tries=2 --retry-connrefused "http://www.google.com:81/not-there"
converted 'http://www.google.com:81/not-there' (ANSI_X3.4-1968) -> 'http://www.google.com:81/not-there' (UTF-8)
--2017-04-27 19:50:28-- http://www.google.com:81/not-there
Resolving www.google.com (www.google.com)... 216.58.211.164, 2a00:1450:4009:805::2004
Connecting to www.google.com (www.google.com)|216.58.211.164|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|2a00:1450:4009:805::2004|:81... failed: Cannot assign requested address.
Retrying.
--2017-04-27 19:50:29-- (try: 2) http://www.google.com:81/not-there
Connecting to www.google.com (www.google.com)|216.58.211.164|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|2a00:1450:4009:805::2004|:81... failed: Cannot assign requested address.
Giving up.
root@8c59d6dd05fe:/var/www/html# wget --timeout=1 --waitretry=0 --tries=2 --retry-connrefused "http://www.google.com:81/not-there"
converted 'http://www.google.com:81/not-there' (ANSI_X3.4-1968) -> 'http://www.google.com:81/not-there' (UTF-8)
--2017-04-27 19:50:35-- http://www.google.com:81/not-there
Resolving www.google.com (www.google.com)... 209.85.203.104, 209.85.203.147, 209.85.203.106, ...
Connecting to www.google.com (www.google.com)|209.85.203.104|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.147|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.106|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.103|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.105|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.99|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|2a00:1450:400b:c03::68|:81... failed: Cannot assign requested address.
Retrying.
--2017-04-27 19:50:41-- (try: 2) http://www.google.com:81/not-there
Connecting to www.google.com (www.google.com)|209.85.203.104|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.147|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.106|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.103|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.105|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|209.85.203.99|:81... failed: Connection timed out.
Connecting to www.google.com (www.google.com)|2a00:1450:400b:c03::68|:81... failed: Cannot assign requested address.
Giving up.
I thought I was going a bit crazy, but it was only when @Socowi pointed out in the comments that the IPs each retry were different that it clicked. It depends on the number of possible IPs being returned. The magic number 7 (retries) I was seeing was number the IP options. When I just choose one specific IP it only tries it once.
我以为我有点疯了,但只有当@Socowi 在评论中指出每次重试的 IP 不同时才点击。这取决于返回的可能 IP 的数量。我看到的神奇数字 7(重试)是 IP 选项的编号。当我只选择一个特定的 IP 时,它只会尝试一次。
回答by Socowi
Wrong Number of Retries
错误的重试次数
Your wget seems to resolve the URL to multiple IP addresses as seen in the second line of your wget
's output. Each IP is then tested with the specified timeout. Unfortunately I haven't found any options to limit the DNS lookup to one address or set a total timeout for all IPs together. But you could try to use "<googles ip address>:81/not-there"
instead of the URL.
您的 wget 似乎将 URL 解析为多个 IP 地址,如您wget
输出的第二行所示。然后使用指定的超时测试每个 IP。不幸的是,我没有找到任何选项来将 DNS 查找限制为一个地址或为所有 IP 设置总超时。但是您可以尝试使用"<googles ip address>:81/not-there"
代替 URL。
Seemingly Too Long Timeout
看似过长的超时
As you already found out, setting --retry-connrefused
lets wget
retry even after a ?connection refused? error. The specified timeout is used for each retry, but between the retries there will be a pause which gets longer after each retry.
正如你已经发现,设置--retry-connrefused
让wget
即使重试?连接被拒绝?错误。每次重试都使用指定的超时时间,但在重试之间会有一个暂停,每次重试后都会变得更长。
Example
例子
wget --timeout=1 --tries=5 --retry-connrefused URL
does something like
做类似的事情
try to connect for 1 second
failed -> wait 1 second
try to connect for 1 second
failed -> wait 2 seconds
try to connect for 1 second
failed -> wait 3 second
try to connect for 1 second
failed -> wait 4 second
try to connect for 1 second
Therefore the command takes tries * timeout + 1 + 2 + ... + (tries - 1)
seconds. This behavior is specified in man wget
under the option, which allows you to change it :)
因此该命令需要tries * timeout + 1 + 2 + ... + (tries - 1)
几秒钟。此行为man wget
在选项下指定,它允许您更改它:)
--waitretry=seconds
If you don't want Wget to wait between every retrieval, but only between retries of failed downloads, you can use this option. Wget will use linear backoff, waiting 1 second after the first failure on a given file, then waiting 2 seconds after the second failure on that file, up to the maximum number of seconds you specify.By default, Wget will assume a value of 10 seconds.
--waitretry=seconds
如果您不希望 Wget 在每次检索之间等待,而只在下载失败的重试之间等待,则可以使用此选项。Wget 将使用线性退避,在给定文件第一次失败后等待 1 秒,然后在该文件第二次失败后等待 2 秒,直到您指定的最大秒数。默认情况下,Wget 将假定值为 10 秒。
I think you wanted to use something like
我想你想使用类似的东西
wget --timeout=1 --waitretry=0 --tries=5 --retry-connrefused URL
which eliminates the pause between two retries, resulting in a total time of timeout * tries
.
这消除了两次重试之间的暂停,导致总时间为timeout * tries
.