Spaces:
Sleeping
Sleeping
# Use this script to test if a given URL is responding | |
# | |
# Usage: | |
# ./wait-for-it.sh --url http://0.0.0.0:8000/health --strict -- node /app/app/server.js | |
url="" | |
cmd="" | |
strict="" | |
while [[ $# -gt 0 ]] | |
do | |
key="$1" | |
case $key in | |
--url) | |
url="$2" | |
shift # past argument | |
shift # past value | |
;; | |
--) | |
shift | |
cmd="$@" | |
break | |
;; | |
--strict) | |
strict="true" | |
shift # past argument | |
;; | |
*) | |
echo "Unknown option: $key" | |
exit 1 | |
;; | |
esac | |
done | |
if [[ -z "$url" ]]; then | |
echo "URL is required" | |
exit 1 | |
fi | |
wait_for_url() { | |
response=$(curl --write-out "%{http_code}" --silent --output /dev/null "$1") | |
if [[ "$response" -eq 200 ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
if [[ "$strict" == "true" ]]; then | |
echo "wait-for-it.sh: waiting for $url without a timeout" | |
while ! wait_for_url "$url"; do | |
sleep 1 | |
done | |
echo "wait-for-it.sh: $url is available" | |
else | |
echo "wait-for-it.sh: waiting for $url with a timeout of 15 seconds" | |
timeout=15 | |
while ! wait_for_url "$url"; do | |
sleep 1 | |
timeout=$((timeout - 1)) | |
if [[ $timeout -eq 0 ]]; then | |
echo "wait-for-it.sh: timeout occurred after waiting for $url" | |
exit 1 | |
fi | |
done | |
echo "wait-for-it.sh: $url is available" | |
fi | |
exec $cmd | |