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
wget force retry until there is a connection
提问by Mika
I have a bash script that uses wget
running 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 wget
stops 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 wget
I can use. If there is not what the best variable to build the loop on?
有没有办法强制重试直到连接恢复?我知道我可以写一个循环,但我想知道是否有wget
我可以使用的设置。如果没有什么最好的变量来构建循环?
More details:
更多细节:
With the code I wrote, wget
will 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:这个怎么运作:
- In case there is no network connection, the
while loop
will not break and it will run thewget
command continuously and keep printing error message. - As soon as it gets connected to the Internet,
wget
starts resolving the host and getting the files. - Now if the connection is lost or some other error occurs, default retry (don't use
0
orinf
i.e unlimited retry, use limited value) ofwget
will retry to get the files until timeout of 15 seconds reached. After 15 seconds thewget
command will fail and print error output and thus thewhile loop
won't break. So it will again reach in a state where there is no connection or such and keep printing error message. - Again as soon as it gets connected or the error is resolved,
wget
starts resolving the host and getting the files. These steps (1-4) continue as long as the files are not downloaded completely. - This
wget
command uses-c
option, i.e resume option. So every instances ofwget
will start (downloading) from where it left off. - When the files get downloaded completely and the
wget
command succeeds, the loop will break.
- 在没有网络连接的情况下
while loop
,它不会中断,它会wget
连续运行命令并不断打印错误信息。 - 一旦它连接到 Internet,
wget
就开始解析主机并获取文件。 - 现在,如果连接丢失或发生其他一些错误,默认重试(不使用
0
或inf
即无限重试,使用有限值)wget
将重试获取文件,直到达到 15 秒超时。15 秒后,该wget
命令将失败并打印错误输出,因此while loop
不会中断。所以它会再次进入没有连接等的状态,并继续打印错误信息。 - 再次连接或解决错误后,
wget
开始解析主机并获取文件。只要文件未完全下载,这些步骤 (1-4) 就会继续。 - 该
wget
命令使用-c
选项,即恢复选项。所以每个实例wget
都将从它停止的地方开始(下载)。 - 当文件完全下载并且
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 1000
could 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 可以进行安静的测试,并使用计数和截止日期进行退货。