使用wget命令时如何同时使用--timeout和--tries参数

16

在退出之前,您期望这个命令需要多长时间才能完成?

wget --timeout=1 --tries=2 "http://www.google.com:81/not-there"

我预计超时时间为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.

我不知道它在放弃之前为什么要尝试6次。

看起来tries是用于重试的。如果我设置--retry-connrefused,它至少会进行重试,但总共花费的时间并不是我所期望的。我希望能够决定在超时时尝试多少次。

编辑:

在@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.

编辑二

当两个完全相同的命令表现不同时,我感到非常困惑。

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.

我本以为自己有点疯了,但仅当 @Socowi 在评论中指出每次重试的IP地址不同时,我才恍然大悟。这取决于返回的可能IP地址数。我看到的神奇数字7(重试)是IP选项编号。当我只选择特定的一个IP地址时,它只会尝试一次。

1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
16

重试次数错误

根据您的wget输出的第二行,看起来您的wget似乎将URL解析为多个IP地址。然后,每个IP都会使用指定的超时进行测试。不幸的是,我没有找到任何选项来限制DNS查找到一个地址或设置所有IP的总超时时间。但您可以尝试使用"<谷歌IP地址>:81/not-there",而不是域名。

要自动将域名解析为单个IP地址,您可以使用

wget "http://$(getent hosts www.google.com | sed 's/ .*//;q'):81/not-there"

看起来过长的超时时间

正如您已经发现的那样,设置--retry-connrefused可以让wget在出现“连接被拒绝”的错误后进行重试。指定的超时时间将用于每次重试,但是在重试之间将会有一个暂停时间,每次暂停时间都会变长。

示例

wget --timeout=1 --tries=5 --retry-connrefused URL

做类似于某事的事情

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
因此,该命令需要 tries * timeout + 1 + 2 + ... + (tries - 1) 秒。这种行为在选项下有明确规定,您可以更改它 :)
wget --timeout=1 --waitretry=0 --tries=5 --retry-connrefused URL

该设置消除了两次重试之间的暂停,从而导致总时间为timeout * tries


谢谢你的回答,但是使用你建议的命令仍然会尝试获取7次*尝试。为什么它总是尝试7次超出了我的理解。如果你没有看到我遇到的同样问题,那么可能与版本有关?我使用的是“GNU Wget 1.17.1 built on linux-gnu.”。 - mickadoo
我尝试了GNU Wget 1.15 built on linux-gnuGNU Wget 1.19.1 built on linux-gnu,两者的结果都如我所述。 - Socowi
我已经更新了描述,其中包含了您的建议的输出。 - mickadoo
我不知道是怎么回事,我在一个版本为1.6的Docker容器上尝试了一下,结果还是一样。对于timeout=1和tries=2,它花费了12秒钟。 - mickadoo
2
非常奇怪。很抱歉我无法帮助您。我只有一件事要补充:您的 wget 似乎将 URL 解析为多个 IP 地址(对我来说只有一个),如您的 wget 输出的第二行所示。然后,每个 IP 都会使用指定的超时进行测试。因此,不同的行为可能是由不同的 DNS 服务器引起的,而不是由 wget 引起的。不幸的是,我没有找到任何选项来限制 DNS 查找到一个地址或设置所有 IP 的总超时时间。但是,您可以尝试使用 "<谷歌的 IP 地址>:81/not-there" 而不是 URL 来验证这个理论。 - Socowi
1
我觉得你解决了它 :-D 我已经快疯了,因为连续两个相同的命令产生了不同的输出!这是由于DNS结果造成的。神秘的7次重试是可能返回的IP数量。如果我只选择一个,那么它就像预期的那样工作。我很傻没有注意到每次尝试时IP地址的差异。如果你想把你最后的评论编辑成答案,我会接受它。 - mickadoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,