Last active
March 11, 2016 04:45
-
-
Save seventhskye/e1f4a6bbae3a1e4e9578 to your computer and use it in GitHub Desktop.
A bash while loop to get a URL from $PROTOCOL, $HOST and $DNS_DOMAIN environment variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DNS_DOMAIN=${DNS_DOMAIN:-example.com} | |
HOST=${HOST:-www} | |
HOSTNAME=$HOST.$DNS_DOMAIN | |
PROTOCOL=${PROTOCOL:-https} | |
URL=$PROTOCOL://$HOSTNAME | |
echo " --> Testing for $URL" | |
# Number of requests | |
TIMEOUT=30 | |
# Time to wait between requests | |
SLEEP=1 | |
# Initial HTTP code, should not be 200 | |
HTTP_CODE=500 | |
while [ $TIMEOUT -gt 0 -a $HTTP_CODE -ne 200 ]; do | |
TIMEOUT=$[$TIMEOUT-1] | |
HTTP_CODE=`curl -o /dev/null --silent --head --write-out '%{http_code}\n' $URL` | |
if [ $HTTP_CODE == 200 ]; then | |
break | |
fi | |
echo " --> GET $URL http_code:$HTTP_CODE ($[TIMEOUT-i] remaining) " | |
sleep $SLEEP | |
done | |
if [ $HTTP_CODE == 200 ]; then | |
echo " --> GET $URL was successful" | |
exit 0 | |
else | |
echo " --> All requests to $URL failed, aborting" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment