Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two more options - wait time and also command to use for testing #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions wait-for-it.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Usage:
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-c COMMAND | --command=COMMAND
A single command to test
-w TIMEOUT | --wait=TIMEOUT Wait / sleep time between each test attempt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait is a very confusing choice of wording in this context. Maybe call it something like interval instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I renamed the name of the option.

-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Expand All @@ -32,19 +35,28 @@ wait_for()
WAITFORIT_start_ts=$(date +%s)
while :
do
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
if [[ $WAITFORIT_COMMAND != "" ]]; then
echoerr "Testing command $WAITFORIT_COMMAND"
$WAITFORIT_COMMAND
WAITFORIT_result=$?
else
(echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
echoerr "Testing on busybox against $WAITFORIT_HOST/$WAITFORIT_PORT"
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
WAITFORIT_result=$?
else
echoerr "Testing against $WAITFORIT_HOST/$WAITFORIT_PORT"
(echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
WAITFORIT_result=$?
fi
fi
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
sleep $WAITFORIT_SLEEP_TIME
echoerr "Sleeping for $WAITFORIT_SLEEP_TIME"
done
return $WAITFORIT_result
}
Expand All @@ -53,9 +65,9 @@ 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 &
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --command=$WAITFORIT_COMMAND --timeout=$WAITFORIT_TIMEOUT &
else
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --command=$WAITFORIT_COMMAND --timeout=$WAITFORIT_TIMEOUT &
fi
WAITFORIT_PID=$!
trap "kill -INT -$WAITFORIT_PID" INT
Expand All @@ -81,6 +93,24 @@ do
WAITFORIT_CHILD=1
shift 1
;;
-c)
WAITFORIT_COMMAND="$2"
if [[ $WAITFORIT_COMMAND == "" ]]; then break; fi
shift 2
;;
--command=*)
WAITFORIT_COMMAND="${1#*=}"
shift 1
;;
-w)
WAITFORIT_SLEEP_TIME="$2"
if [[ $WAITFORIT_SLEEP_TIME == "" ]]; then break; fi
shift 2
;;
--wait=*)
WAITFORIT_SLEEP_TIME="${1#*=}"
shift 1
;;
-q | --quiet)
WAITFORIT_QUIET=1
shift 1
Expand Down Expand Up @@ -131,8 +161,8 @@ do
esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
if [[ ("$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "") && "$WAITFORIT_COMMAND" == "" ]]; then
echoerr "Error: you need to provide a host and port, or a single command to test."
usage
fi

Expand Down