Search code examples
curlzsh

Make cURL resume interrupted downloads using the 'until' loop


Here is a solution how to make cURL automatically resume interrupted downloads, from the 2013 blog post by Juan Nunez-Iglesias:

export ec=18; while [ $ec -eq 18 ]; do curl -O -C - http://site.xyz/file.zip; export ec=$?; done

It has been posted several times here, both the original version and with slight changes inside the while loop.

The changes inside the while loop were introduced by different people to make the script more robust.

  • "The exit code curl chucks when a download is interrupted is 18, and $? gives you the exit code of the last command in bash. So, while the exit code is 18, keep trying to download the file, maintaining the filename (-O) and resuming where the previous download left off (-C)." - Juan Nunez-Iglesias
  • "As Jan points out in the comments, depending what is going wrong with the download, your error code may be different. Just change 18 to whatever error you're seeing to get it to work for you! (If you're feeling adventurous, you could change the condition to while [ $ec -ne 0 ], but that feels like using a bare except in Python, which is bad. ;)" - Juan Nunez-Iglesias
  • "In my case curl exits with different codes, so I check for success code instead: while [ $ec -ni 0 ]" - Ilya Belsky

In March, Helder Magalhães suggested to replace the while loop in the original Juan's version with until:

This solution can be further simplified using until unstead. See related thread.

Though I'm not really good in using cURL or in shell scripting, I would try to write it myself, but the problem to write it myself is not the script itself but the lack of understanding how to properly test it.

Could somebody show how to implement Helder's idea? And does it really make script more robust?

I use Zsh.


Solution

  • Here you go:

    until curl ...; do
      [ $? -eq 18 ] || break
    done