Skip to content

Instantly share code, notes, and snippets.

@efrecon efrecon/wait-for.sh
Last active Nov 9, 2018

Embed
What would you like to do?
Wait for a port at remote site to be opened. Same as https://github.com/Eficode/wait-for but with options to be silent and an exponential backoff when waiting.
#!/bin/sh
# This comes from : https://github.com/Eficode/wait-for
# The MIT License (MIT)
#
# Copyright (c) 2017 Eficode Oy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
TIMEOUT=120
QUIET=0
VERBOSE=0
WAIT=1
SPEED=2
BACKOFF=15
echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
}
usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$cmdname host:port [-t timeout] [-- command args]
-v | --verbose Be more verbose when waiting
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-w WAIT | --wait=seconds Number of seconds to wait between attempts
-b BACKOFF | --backoff=seconds Maximum number of seconds to wait for exponential backoff
-s SPEED | --speed=float Seconds multiplier when exponential backoff.
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit "$exitcode"
}
wait_once() {
NC=`which nc`
BASH=`which bash`
if [ -n "$NC" ]; then
if [ "$VERBOSE" = "1" -a "$QUIET" = "0" ]; then
echo "Waiting for ${HOST}:${PORT} with ${NC}..." 1>&2
fi
${NC} -z -w 5 "$HOST" "$PORT" > /dev/null 2>&1
elif [ -n "$BASH" ]; then
if [ "$VERBOSE" = "1" -a "$QUIET" = "0" ]; then
echo "Waiting for ${HOST}:${PORT} with ${BASH}..." 1>&2
fi
timeout 5 ${BASH} -c "echo >/dev/tcp/${HOST}/${PORT}" >/dev/null 2>&1
else
echo "CANNOT test connection to ${HOST}:${PORT}!"
exit 0
fi
result=$?
if [ $result -eq 0 ] ; then
if [ $# -gt 0 ] ; then
if [ "$VERBOSE" = "1" -a "$QUIET" = "0" ]; then
echo "Connection to ${HOST}:${PORT} established" 1>&2
fi
exec "$@"
fi
exit 0
fi
sleep ${WAIT}
}
adjust() {
if [ "${SPEED}" -ne "1" ]; then
WAIT=$(awk "BEGIN{print int($WAIT * $SPEED)}")
if [ "${WAIT}" -gt "${BACKOFF}" ]; then
WAIT="${BACKOFF}"
fi
fi
}
wait_for() {
if [ "$TIMEOUT" = "" -o "$TIMEOUT" -le "0" ]; then
while true; do
wait_once "$@"
adjust
done
else
waited=0
while [ "$waited" -lt "${TIMEOUT}" ]; do
wait_once "$@"
waited=$(expr $waited + ${WAIT})
adjust
done
fi
echo "Operation timed out" >&2
exit 1
}
while [ $# -gt 0 ]
do
case "$1" in
*:* )
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
shift 1
;;
-q | --quiet)
QUIET=1
shift 1
;;
-v | --verbose)
VERBOSE=1
shift 1
;;
-t)
TIMEOUT="$2"
if [ "$TIMEOUT" = "" ]; then break; fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
-w)
WAIT="$2"
shift 2
;;
--wait=*)
WAIT="${1#*=}"
shift 1
;;
-b)
BACKOFF="$2"
shift 2
;;
--backoff=*)
BACKOFF="${1#*=}"
shift 1
;;
-s)
SPEED="$2"
shift 2
;;
--speed=*)
SPEED="${1#*=}"
shift 1
;;
--)
shift
break
;;
--help)
usage 0
;;
*)
echoerr "Unknown argument: $1"
usage 1
;;
esac
done
if [ "$HOST" = "" -o "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
fi
wait_for "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.