bash wget 强制重试直到有连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30983511/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:14:45  来源:igfitidea点击:

wget force retry until there is a connection

linuxbashwget

提问by Mika

I have a bash script that uses wgetrunning on a 3g device. I want the script to pause until I get a response so I have setup my code like this:

我有一个wget在 3g 设备上运行的 bash 脚本。我希望脚本暂停,直到我得到响应,所以我设置了这样的代码:

wget -t 0 -T 15 --retry-connrefused www.example.com

The problem is that when there is no connection at all (3g drops for a few seconds) the DNS can't resolve the host and wgetstops retrying.

问题是,当根本没有连接(3g 掉线几秒钟)时,DNS 无法解析主机并wget停止重试。

Is there a way to force retry until the connection comes back? I know I can write a loop but I want to know if there is a setting in wgetI can use. If there is not what the best variable to build the loop on?

有没有办法强制重试直到连接恢复?我知道我可以写一个循环,但我想知道是否有wget我可以使用的设置。如果没有什么最好的变量来构建循环?

More details:

更多细节:

With the code I wrote, wgetwill retry but if the device has no Internet connection at all (for example I pull out the 3g dongle from the device) it will stop retrying telling me it can't resolve the host address. It sends that after the 15 seconds defined by -T 15

使用我编写的代码,wget将重试,但如果设备根本没有互联网连接(例如,我从设备中拔出 3g 加密狗),它将停止重试,告诉我它无法解析主机地址。它在定义的 15 秒后发送-T 15

wget: unable to resolve host address

回答by Jahid

This loop should do this:

这个循环应该这样做:

while true;do
wget -T 15 -c http://example.com && break
done



How it works:这个怎么运作:

  1. In case there is no network connection, the while loopwill not break and it will run the wgetcommand continuously and keep printing error message.
  2. As soon as it gets connected to the Internet, wgetstarts resolving the host and getting the files.
  3. Now if the connection is lost or some other error occurs, default retry (don't use 0or infi.e unlimited retry, use limited value) of wgetwill retry to get the files until timeout of 15 seconds reached. After 15 seconds the wgetcommand will fail and print error output and thus the while loopwon't break. So it will again reach in a state where there is no connection or such and keep printing error message.
  4. Again as soon as it gets connected or the error is resolved, wgetstarts resolving the host and getting the files. These steps (1-4) continue as long as the files are not downloaded completely.
  5. This wgetcommand uses -coption, i.e resume option. So every instances of wgetwill start (downloading) from where it left off.
  6. When the files get downloaded completely and the wgetcommand succeeds, the loop will break.
  1. 在没有网络连接的情况下while loop,它不会中断,它会wget连续运行命令并不断打印错误信息。
  2. 一旦它连接到 Internet,wget就开始解析主机并获取文件。
  3. 现在,如果连接丢失或发生其他一些错误,默认重试(不使用0inf即无限重试,使用有限值)wget将重试获取文件,直到达到 15 秒超时。15 秒后,该wget命令将失败并打印错误输出,因此while loop不会中断。所以它会再次进入没有连接等的状态,并继续打印错误信息。
  4. 再次连接或解决错误后,wget开始解析主机并获取文件。只要文件未完全下载,这些步骤 (1-4) 就会继续。
  5. wget命令使用-c选项,即恢复选项。所以每个实例wget都将从它停止的地方开始(下载)。
  6. 当文件完全下载并且wget命令成功时,循环将中断。

回答by jgr208

Here is a script that you can use to resolve your problem

这是您可以用来解决问题的脚本

FILENAME=
DOWNURL=

wget -O "`echo $FILENAME`" "`echo $DOWNURL`"
FILESIZE=$(stat -c%s "$FILENAME")

while [ $FILESIZE \< 1000 ]; do
    sleep 3
    wget -O "`echo $FILENAME`" "`echo $DOWNURL`"
    FILESIZE=$(stat -c%s "$FILENAME")
done

The script forces the download to continue until it finishes. The 1000could be changed to fit whatever size file you are downloading.

该脚本强制下载继续,直到下载完成。将1000可以改变,以适应任何大小的文件,你正在下载。

回答by Neil H Watson

I would not use a loop. You could end up with multiple downloads. Rather invent smarter way to check the network connection. Try pinging some sites beforehand and issue wget only if they succeed. If they fail the script could wait and try the test again. Ping can do quiet tests and use counts and deadlines for returns.

我不会使用循环。您最终可能会进行多次下载。而是发明更智能的方法来检查网络连接。尝试预先 ping 一些站点,并仅在它们成功时才发出 wget。如果它们失败,脚本可以等待并再次尝试测试。Ping 可以进行安静的测试,并使用计数和截止日期进行退货。