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

Bil*_*big 27 linux curl resume-download

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

我已经使用过这个命令了,但它不起作用:

until curl -r 666-9999 -C - --retry 999 -o "path/to/file" "ftp:/path/to/remote/file"; do :; done
Run Code Online (Sandbox Code Playgroud)

Kev*_*RED 35

curl -L -O your_url
Run Code Online (Sandbox Code Playgroud)

这将下载该文件.

现在让我们说你的连接中断;

curl -L -O -C - your_url
Run Code Online (Sandbox Code Playgroud)

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

从联机帮助页:

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

  • 是的,如果你想用 --output 指定一个输出文件,这也可以。使用重定向(`> myfile`),它不会(显然) (5认同)

小智 14

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.
Run Code Online (Sandbox Code Playgroud)

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

  • @Geremia如果建议解决了潜在的问题 - 强力下载文件 - 为什么不分享? (5认同)
  • 问题是关于`curl`,而不是'wget`. (3认同)
  • 根据联机帮助页,它不支持仅下载文件的一部分,但`-c`标志允许继续下载. (2认同)

Cla*_*ani 11

您可以在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
Run Code Online (Sandbox Code Playgroud)

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

  • 在我的例子中,“curl”与其他代码一起退出,因此我改为检查成功代码:“export ec=18;” 而 [ $ec -ni 0 ]; 执行 /usr/bin/curl -O -C - "http://www.example.com/a-big-archive.zip"; 导出 ec=$?; 完成` (3认同)