Skip to content

Instantly share code, notes, and snippets.

@0x2539
Last active January 9, 2019 16:38
Show Gist options
  • Save 0x2539/f71579e69db682b1c5fc29fc59bf7226 to your computer and use it in GitHub Desktop.
Save 0x2539/f71579e69db682b1c5fc29fc59bf7226 to your computer and use it in GitHub Desktop.
A bash script for health checking a URL. See the start of the script for instructions in how to run it.
#!/usr/bin/env bash
# How to run:
# 1) chmod +x health_checking.sh
# 2) ./health_checking.sh --url=http://localhost:8000 --timeout=60
# Define a timestamp function
timestamp() {
date +"%s"
}
# Define a date function
dateString() {
date +"%T"
}
startTs=$(timestamp)
echo "started at $(dateString) ($(timestamp))"
for i in "$@"
do
case ${i} in
--url=*)
url="${i#*=}"
;;
--timeout=*)
timeout="${i#*=}"
;;
--body=*)
body="${i#*=}"
;;
--method=*)
method="${i#*=}"
;;
*)
echo "Unknown option ${i}"
echo "Ended at $(dateString) ($(timestamp)) (duration: $(($(timestamp)-${startTs})) seconds)"
exit -1
;;
esac
done
if [[ -z "${url}" ]]; then
echo "url is missing, set it as environment variable or pass it as argument ('./health_checking.sh --url=http://localhost:8000 --method=GET --body='{\"id\": 1}' --timeout=60' or 'export url=http://localhost:8000')"
echo "Ended at $(dateString) ($(timestamp)) (duration: $(($(timestamp)-${startTs})) seconds)"
exit -1
fi
if [[ -z "${body}" ]]; then
body=''
echo "body is missing, using empty body. You can set it as environment variable or pass it as argument ('./health_checking.sh --url=http://localhost:8000 --method=GET --body='{\"id\": 1}' --timeout=60' or export body='{\"id\": 1}')"
fi
if [[ -z "${method}" ]]; then
method="GET"
echo "method is missing, using default '${method}'. You can set it as environment variable or pass it as argument ('./health_checking.sh --url=http://localhost:8000 --method=GET --body='{\"id\": 1}' --timeout=60' or 'export method=${method}')"
fi
if [[ -z "${timeout}" ]]; then
timeout=60
echo "timeout is missing, using default '${timeout}' seconds. You can set it as environment variable or pass it as argument ('./health_checking.sh --url=http://localhost:8000 --method=GET --body='{\"id\": 1}' --timeout=60' or 'export timeout=${timeout}')"
fi
echo "Health checking for url: '${url}', with timeout: '${timeout}' seconds."
response="0"
sleepTime=0 # do not sleep the first time we call the url
while [[ ${response} -ne "200" ]]
do
# check if we exceeded the timeout
totalTimeSpent=$(($(timestamp)-${startTs}))
if [[ "$totalTimeSpent" -ge "$timeout" ]]; then
echo "Timeout Error! Didn't receive a success in ${timeout} seconds."
exit -1
fi
sleep ${sleepTime}
sleepTime=1 # before calling the url next time sleep for 1 second
if [[ -z "${body}" ]]; then
response=$(curl -X ${method} --write-out "%{http_code}\n" --silent --output /dev/null "$url")
else
response=$(curl -X ${method} -d "${body}" --write-out "%{http_code}\n" --silent --output /dev/null "$url")
fi
echo "Response waiting for '200', got: '$response'"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment