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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s