diff --git a/README.md b/README.md index f1956f7..f169999 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your ``` ./wait-for host:port [-t timeout] [-- command args] -q | --quiet Do not output any status messages + -s | --strict Only execute subcommand if the test succeeds -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes ``` @@ -26,6 +27,15 @@ Connection to www.eficode.com port 80 [tcp/http] succeeded! Eficode site is up ``` +The subcommand will be executed regardless if the service is up or not. If you wish to execute the subcommand only if the service is up, add the --strict argument. In this example, we will test port 81 on www.google.com which will fail: + +``` +$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up" +$ ./wait-for www.google.com:81 --timeout=1 --strict -- echo "google is up" +Operation timed out +google is up +``` + To wait for database container to become available: diff --git a/wait-for b/wait-for index ddfc39e..ae2c850 100755 --- a/wait-for +++ b/wait-for @@ -13,6 +13,7 @@ usage() { Usage: $cmdname host:port [-t timeout] [-- command args] -q | --quiet Do not output any status messages + -s | --strict Only execute subcommand if the test succeeds -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes USAGE @@ -33,6 +34,12 @@ wait_for() { sleep 1 done echo "Operation timed out" >&2 + if [ $result -ne 0 ] && [ $STRICT -ne 1 ] ; then + if [ $# -gt 0 ] ; then + exec "$@" + fi + exit 0 + fi exit 1 } @@ -48,6 +55,10 @@ do QUIET=1 shift 1 ;; + -s | --strict) + STRICT=1 + shift 1 + ;; -t) TIMEOUT="$2" if [ "$TIMEOUT" = "" ]; then break; fi @@ -76,4 +87,6 @@ if [ "$HOST" = "" -o "$PORT" = "" ]; then usage 2 fi +STRICT=${STRICT:-0} + wait_for "$@"