Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env sh
- TIMEOUT=15
- INTERVAL=1
- QUIET=0
- 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]
- -q | --quiet Do not output any status messages
- -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
- -i INTERVAL | --interval=interval Time in seconds between retries
- -- COMMAND ARGS Execute command with args after the test finishes
- USAGE
- exit "$exitcode"
- }
- wait_for() {
- local t=$TIMEOUT
- while [ $t -gt 0 ] || [ $TIMEOUT -eq 0 ]; do
- nc -z "$HOST" "$PORT" > /dev/null 2>&1
- result=$?
- if [ $result -eq 0 ] ; then
- if [ $# -gt 0 ] ; then
- exec "$@"
- fi
- echoerr "$HOST":"$PORT" is available
- exit 0
- fi
- echoerr "$HOST":"$PORT" is not available
- sleep $INTERVAL
- t=$((t-$INTERVAL))
- done
- echoerr "Operation timed out"
- exit 1
- }
- while getopts "qt:i:" arg; do
- case "$arg" 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
- ;;
- -t) # timeout. Requires passed value.
- TIMEOUT="$2"
- if [ "$TIMEOUT" = "" ]; then break; fi
- shift 2
- ;;
- -i) # interval. Requires passed value.
- INTERVAL="$2"
- if [ "$INTERVAL" = "" ]; then break; fi
- shift 2
- ;;
- -h) # help.
- usage 0
- ;;
- *)
- echoerr "Unknown argument: $1"
- usage 1
- ;;
- esac
- done
- if [ "$HOST" = "" -o "$PORT" = "" ]; then
- echoerr "Error: Host and port need to be provided."
- usage 2
- fi
- wait_for "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement