Skip to content

Instantly share code, notes, and snippets.

@ypereirareis
Last active August 19, 2016 05:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypereirareis/964cd2d2b608faa371f5 to your computer and use it in GitHub Desktop.
Save ypereirareis/964cd2d2b608faa371f5 to your computer and use it in GitHub Desktop.
Script to wait for a host to become available locally
wait_single_host() {
local host=$1
shift
local port=$1
shift
echo "==> Check host ${host}:${port}"
while ! nc ${host} ${port} > /dev/null 2>&1 < /dev/null; do echo " --> Waiting for ${host}:${port}" && sleep 1; done;
}
wait_all_hosts() {
if [ ! -z "$WAIT_FOR_HOSTS" ]; then
local separator=':'
for _HOST in $WAIT_FOR_HOSTS ; do
IFS="${separator}" read -ra _HOST_PARTS <<< "$_HOST"
wait_single_host "${_HOST_PARTS[0]}" "${_HOST_PARTS[1]}"
done
else
echo "IMPORTANT : Waiting for nothing because no $WAIT_FOR_HOSTS env var defined !!!"
fi
}
wait_all_hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment