Created
March 15, 2022 10:44
-
-
Save sdumetz/5021bfd6b4ec2f13bfc06a131b0c0b9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Use this script to test if a given TCP host/port are available | |
WAITFORIT_cmdname=${0##*/} | |
echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } | |
usage() | |
{ | |
cat << USAGE >&2 | |
Usage: | |
$WAITFORIT_cmdname [-s] [-t timeout] [-- command args] | |
-s | --strict Only execute subcommand if the test succeeds | |
-q | --quiet Don't output any status messages | |
-t TIMEOUT | --timeout=TIMEOUT | |
Timeout in seconds, zero for no timeout | |
-- COMMAND ARGS Execute command with args after the test finishes | |
USAGE | |
exit 1 | |
} | |
wait_for() | |
{ | |
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then | |
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for X11 on display $DISPLAY" | |
else | |
echoerr "$WAITFORIT_cmdname: waiting for X11 on display $DISPLAY without a timeout" | |
fi | |
WAITFORIT_start_ts=$(date +%s) | |
while : | |
do | |
(xset -q) >/dev/null 2>$1 | |
WAITFORIT_result=$? | |
if [[ $WAITFORIT_result -eq 0 ]]; then | |
WAITFORIT_end_ts=$(date +%s) | |
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" | |
break | |
fi | |
sleep 1 | |
done | |
return $WAITFORIT_result | |
} | |
wait_for_wrapper() | |
{ | |
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 | |
if [[ $WAITFORIT_QUIET -eq 1 ]]; then | |
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & | |
else | |
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & | |
fi | |
WAITFORIT_PID=$! | |
trap "kill -INT -$WAITFORIT_PID" INT | |
wait $WAITFORIT_PID | |
WAITFORIT_RESULT=$? | |
if [[ $WAITFORIT_RESULT -ne 0 ]]; then | |
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" | |
fi | |
return $WAITFORIT_RESULT | |
} | |
# process arguments | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
-q | --quiet) | |
WAITFORIT_QUIET=1 | |
shift 1 | |
;; | |
-t) | |
WAITFORIT_TIMEOUT="$2" | |
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi | |
shift 2 | |
;; | |
--timeout=*) | |
WAITFORIT_TIMEOUT="${1#*=}" | |
shift 1 | |
;; | |
--) | |
shift | |
WAITFORIT_CLI=("$@") | |
break | |
;; | |
--help) | |
usage | |
;; | |
*) | |
echoerr "Unknown argument: $1" | |
usage | |
;; | |
esac | |
done | |
WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} | |
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} | |
# Check to see if timeout is from busybox? | |
WAITFORIT_TIMEOUT_PATH=$(type -p timeout) | |
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) | |
WAITFORIT_BUSYTIMEFLAG="" | |
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then | |
# Check if busybox timeout uses -t flag | |
# (recent Alpine versions don't support -t anymore) | |
if timeout &>/dev/stdout | grep -q -e '-t '; then | |
WAITFORIT_BUSYTIMEFLAG="-t" | |
fi | |
fi | |
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then | |
wait_for_wrapper | |
WAITFORIT_RESULT=$? | |
else | |
wait_for | |
WAITFORIT_RESULT=$? | |
fi | |
if [[ $WAITFORIT_CLI != "" ]]; then | |
exec "${WAITFORIT_CLI[@]}" | |
else | |
exit $WAITFORIT_RESULT | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment