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

时间:2013-11-01 14:37:50

标签: linux curl resume-download

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

我已经使用过这个命令了,但它没有用:

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

3 个答案:

答案 0 :(得分:21)

curl -L -O your_url

这将下载文件。

现在假设你的连接被中断了;

curl -L -O -C - your_url

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

从联系手册:

  

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

答案 1 :(得分:12)

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几乎适用于所有Linux发行版 - 它可能已经安装在你的发行版上。只需使用wget下载文件,它将重新建立网络连接,直到文件完全传输。

答案 2 :(得分:8)

您可以在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

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