Skip to content

Instantly share code, notes, and snippets.

@fmamud
Last active April 26, 2021 14:27
Show Gist options
  • Save fmamud/069cdd4ac15d651c44451238863e2691 to your computer and use it in GitHub Desktop.
Save fmamud/069cdd4ac15d651c44451238863e2691 to your computer and use it in GitHub Desktop.
Adding HTTP check
#!/bin/bash
set -e
sleep_seconds=2
function usage() {
echo "Usage: ./wait <tcp/http> <host>:<port>"
}
function check() {
if [[ -z "$1" ]]; then
echo "Error: invalid $2"
usage
exit 2
fi
}
function tcp() {
IFS=: read -r host port <<< "$1"
while ! nc -z $host $port; do
echo "Waiting TCP connection to $host:$port"
sleep $sleep_seconds
done
}
function http() {
while [[ "$(curl -s -o /dev/null -I -w ''%{http_code}'' $1)" != "200" ]]; do echo "Waiting HTTP connection to $1" && sleep $sleep_seconds; done
}
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
check "$2" "host:port"
case "$1" in
'tcp') tcp $2 $3 ;;
'http') http $2 $3 ;;
*) usage ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment