Linux 如何在curl中自动恢复中断的下载?

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

How to resume interrupted download automatically in curl?

linuxcurlresume-download

提问by Bili the big

I'm working with curlin Linux. I'm downloading a part of a file in ftp server (using the -roption), but my connection is not good, it always interrupts. I want to write a script which resume download when I'm connected again.

curl在 Linux 中工作。我正在 ftp 服务器中下载文件的一部分(使用-r选项),但我的连接不好,它总是中断。我想编写一个脚本,当我再次连接时恢复下载。

I've used this command, but it's not working:

我用过这个命令,但它不起作用:

until curl -r 666-9999 -C - --retry 999 -o "path/to/file" "ftp:/path/to/remote/file"; do :; done

回答by sjaensch

wget has been built specifically for this use case. From the man page:

wget 是专门为此用例构建的。从手册页:

Wget has been designed for robustness over slow or unstable network connections;
if a download fails due to a network problem, it will keep retrying until the
whole file has been retrieved.  If the server supports regetting, it will
instruct the server to continue the download from where it left off.

wget is available for almost all Linux distributions - it probably is already installed on yours. Just use wget to download the file, it will re-establish the network connection until the file is completely transferred.

wget 几乎可用于所有 Linux 发行版 - 它可能已经安装在您的发行版上。只需使用 wget 下载文件,它就会重新建立网络连接,直到文件完全传输完毕。

回答by Claudio Floreani

You can check the exit code in a while loop and resume until the exit code indicates that the download has succeeded:

您可以在while循环中检查退出代码并继续,直到退出代码表明下载成功:

export ec=18; while [ $ec -eq 18 ]; do /usr/bin/curl -O -C - "http://www.example.com/a-big-archive.zip"; export ec=$?; done

The example is taken from http://ilovesymposia.com/2013/04/11/automatically-resume-interrupted-downloads-in-osx-with-curl/

该示例取自http://ilovesymposia.com/2013/04/11/automatically-resume-interrupted-downloads-in-osx-with-curl/

回答by Kevin RED

curl -L -O your_url

This will download the file.

这将下载文件。

Now let's say your connection is interrupted;

现在假设您的连接中断;

curl -L -O -C - your_url

This will continue downloading from the last byte downloaded

这将从下载的最后一个字节继续下载

From the manpage:

从联机帮助页:

Use "-C -" to tell curl to automatically find out where/how to resume the transfer. It then uses the given output/input files to figure that out.

使用“-C -”告诉 curl 自动找出恢复传输的位置/方式。然后它使用给定的输出/输入文件来解决这个问题。