Continuous Download Script Utilizing Bash

I had been having an issue getting a file from a central server halfway across the world and it kept failing every 5 minutes.

To remedy this I put a curl command inside a while loop and made it keep attempting to download the file until it successfully downloaded.

As a result, I came up with the below script, and it worked very nicely:

URL_TO_DOWNLOAD_FROM="http://URL.COM/download.zip"
FINAL_FILE_NAME="MyFile.zip"

EXIT_CODE=1

while [ $EXIT_CODE -ne 0 ]
do
	echo "Downloading File - $FINAL_FILE_NAME from $URL_TO_DOWNLOAD_FROM ..."
	rm -Rf $FINAL_FILE_NAME
	curl $URL_TO_DOWNLOAD_FROM --output $FINAL_FILE_NAME
	
	EXIT_CODE=$?
	
	if [ $EXIT_CODE -ne 0 ]; then
		echo "Download Failed! Retrying..."
	fi
done

if [ $EXIT_CODE -eq 0 ]; then
	echo "Download Finished!"
fi 

Leave a comment