Last active
October 4, 2018 06:20
-
-
Save romash1408/4eee10c94d8bdbd9b37f1d0dbd91461f to your computer and use it in GitHub Desktop.
Repeat request to host using curl untill it return specified http code
This file contains 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/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