oneuptime/Tests/Scripts/endpoint-status.sh
Jack Veney 17a0b65a4b
Update endpoint-status.sh
Included the -L option in the curl command, ensuring that it will follow any 301 redirects until the final URL is reached.
2024-06-20 15:24:27 -04:00

40 lines
1.3 KiB
Bash

#!/bin/bash
# Set the endpoint URL
endpoint_url=$2
name=$1
# if name or endpoint is not provided, exit
if [ $# -eq 0 ]; then
echo "❌ Error: Please provide a name and endpoint URL"
exit 1
fi
# Set the maximum number of retries
max_retries=20 # This allows for 12 attempts, which totals 1 minute with 5 seconds between each retry
retry_interval=15 # Retry interval in seconds
# Initialize variables
retries=0
http_status=0
# Loop until either the endpoint returns 200 or the maximum retries are reached
while [ $retries -lt $max_retries ]; do
# Make a curl request and capture the HTTP status code, following redirects
http_status=$(curl -s -o /dev/null -w "%{http_code}" -L $endpoint_url)
# Check if the HTTP status code is 200 (OK)
if [ $http_status -eq 200 ]; then
echo "$name endpoint is up ($endpoint_url)"
exit 0 # Exit the script with success status
else
echo "$name $endpoint_url returned HTTP $http_status, retrying in $retry_interval seconds. Usually takes few minutes to boot."
sleep $retry_interval
retries=$((retries + 1))
fi
done
# If the loop exits without getting HTTP 200, throw an error
echo "❌ Error: Maximum retries reached, $name still not returning HTTP 200. It could be down or taking longer than expected to start."
exit 1 # Exit the script with an error status