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



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

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

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

这将下载文件。

现在假设您的连接中断;

curl -L -O -C - your_url

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

从手册页:

使用"-C -"告诉curl自动找出在哪里/如何恢复传输。然后,它使用给定的输出/输入文件来解决这个问题。

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 下载文件,它将重新建立网络连接,直到文件完全传输。

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

最新更新