Skip to content

Instantly share code, notes, and snippets.

@horiga
Last active June 1, 2016 13:25
Show Gist options
  • Save horiga/9b07053b7f555322a8edf3b69025ad67 to your computer and use it in GitHub Desktop.
Save horiga/9b07053b7f555322a8edf3b69025ad67 to your computer and use it in GitHub Desktop.
bash script for http status check
#!/bin/bash
# endpoint URI
endpoint=$1
# http execution timeout [sec]
timeout=1
successful_http_code=${2:-200}
execution_fail_code="000"
max_attempts=${3:-5}
echo "endpoint: ${endpoint}"
echo "successful_http_code: ${successful_http_code}"
function connect_to_server {
status=`curl -s ${endpoint} -o /dev/null -w '%{http_code}'`
if [ "000" == $status ];then
echo "connection failed"
return 1
fi
echo "http_status is '${status}'"
[ $successful_http_code -eq $status ] && rtn=0 || rtn=1
return $rtn
}
attempts=0
while [ $attempts -ne $max_attempts ]
do
echo "Try to check...($attempts)"
connect_to_server
results=$?
if [ 0 -eq $results ];then
exit 0
fi
attempts=`expr $attempts + 1`
sleep 1
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment