Skip to content

Instantly share code, notes, and snippets.

@romash1408
Last active October 4, 2018 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romash1408/4eee10c94d8bdbd9b37f1d0dbd91461f to your computer and use it in GitHub Desktop.
Save romash1408/4eee10c94d8bdbd9b37f1d0dbd91461f to your computer and use it in GitHub Desktop.
Repeat request to host using curl untill it return specified http code
#/bin/sh
HOST=${1}
ATTEMPTS=${2:-10} # -1 for unlimited
CODE=${3:-200}
OUT=${4:-/dev/null}
DELAY=${5:-1s}
case "$HOST" in
"" | "-h" | "--help")
echo "
Repeat request to HOST(1) ATTEMPTS(2 = 10) times with DELAY(5 = 1s)
untill it return CODE(3 = 200) http code. Write output to OUT(4 = /dev/null)"
exit 1
;;
esac
if [[ "$OUT" = "/dev/null" ]]; then
GET_BODY="I"
else
GET_BODY=""
fi
CURL="curl -${GET_BODY}so ${OUT} -w %{http_code} ${HOST}"
until [ $ATTEMPTS -eq 0 ]; do
[ `$CURL` -eq $CODE ] && exit 0
[ $ATTEMPTS -gt 0 ] && [ $((--ATTEMPTS)) ]
[ $ATTEMPTS -gt 0 ] && sleep $DELAY
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment