Skip to content

Instantly share code, notes, and snippets.

@eisenreich
Forked from rgl/wait_for_http_200.sh
Last active February 6, 2024 19:03
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save eisenreich/195ab1f05715ec86e300f75d007d711c to your computer and use it in GitHub Desktop.
Save eisenreich/195ab1f05715ec86e300f75d007d711c to your computer and use it in GitHub Desktop.
Wait for HTTP endpoints to return 200 OK with bash, curl and timeout
#!/bin/bash
##############################################################################################
# Wait for URLs until return HTTP 200
#
# - Just pass as many urls as required to the script - the script will wait for each, one by one
#
# Example: ./wait_for_urls.sh "${MY_VARIABLE}" "http://192.168.56.101:8080"
##############################################################################################
wait-for-url() {
echo "Testing: $1"
timeout --foreground -s TERM 30s bash -c \
'while [[ "$(curl -s -o /dev/null -m 3 -L -w ''%{http_code}'' ${0})" != "200" ]];\
do echo "Waiting for ${0}" && sleep 2;\
done' ${1}
local TIMEOUT_RETURN="$?"
if [[ "${TIMEOUT_RETURN}" == 0 ]]; then
echo "OK: ${1}"
return
elif [[ "${TIMEOUT_RETURN}" == 124 ]]; then
echo "TIMEOUT: ${1} -> EXIT"
exit "${TIMEOUT_RETURN}"
else
echo "Other error with code ${TIMEOUT_RETURN}: ${1} -> EXIT"
exit "${TIMEOUT_RETURN}"
fi
}
echo "Wait for URLs: $@"
for var in "$@"; do
echo ""
wait-for-url "$var"
done
echo ""
echo "SUCCESSFUL"
@eisenreich
Copy link
Author

eisenreich commented Jul 28, 2022

Output is:

For successful run:

Wait for URLs: http://google.de http://google.de

Testing: http://google.de
OK: http://google.de

Testing: http://google.de
OK: http://google.de

SUCCESSFUL

For timeout run:

Wait for URLs: http://google.de http://googleeeeee.de http://google.de

Testing: http://google.de
OK: http://google.de

Testing: http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
Waiting for http://googleeeeee.de
TIMEOUT: http://googleeeeee.de -> EXIT

@Smasherr
Copy link

Smasherr commented Aug 3, 2022

Your shebang is wrong ;)
Should be #!/bin/bash or #!/usr/bin/env bash

@tjcdeveloper
Copy link

This script doesn't seem to work properly. If the cURL never succeeds and it times out it still outputs OK!. It should exit 1 if the timeout hits its limit.

@p0z
Copy link

p0z commented Feb 14, 2023

@tjcdeveloper I fork this script and just added a timeout check - is error code 124.
https://gist.github.com/p0z/3894d69f90141a463f790db6da8f6547

@eisenreich
Copy link
Author

Your shebang is wrong ;) Should be #!/bin/bash or #!/usr/bin/env bash

Ty, I added it

@eisenreich
Copy link
Author

This script doesn't seem to work properly. If the cURL never succeeds and it times out it still outputs OK!. It should exit 1 if the timeout hits its limit.
Ty, I added a result handling

@eisenreich
Copy link
Author

@tjcdeveloper I fork this script and just added a timeout check - is error code 124. https://gist.github.com/p0z/3894d69f90141a463f790db6da8f6547

Thank you for the idea - just keep in mind that now each return code except of 124 will return OK

@p0z
Copy link

p0z commented Feb 23, 2023

Thank you for the idea - just keep in mind that now each return code except of 124 will return OK

Yes, you are right. In fact only interested the response code 0, all other codes is error.

TIMEOUT_RETURN="$?"
if [[ "$TIMEOUT_RETURN" == 0 ]]; then
  echo -e "\n[+] ${1} - 200 OK"
else
  echo -e "\n[-] ${1} - timeout or other error! [$TIMEOUT_RETURN]"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment