Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wait-for-it
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joël Schwab
wait-for-it
Commits
54d1f0bf
Commit
54d1f0bf
authored
6 years ago
by
Douglas Gibbons
Browse files
Options
Downloads
Plain Diff
Merge branch 'fwoelffel-master'
parents
4dd67a65
9995b721
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wait-for-it.sh
+67
-66
67 additions, 66 deletions
wait-for-it.sh
with
67 additions
and
66 deletions
wait-for-it.sh
+
67
−
66
View file @
54d1f0bf
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available
cmdname
=
${
0
##*/
}
WAITFORIT_
cmdname
=
${
0
##*/
}
echoerr
()
{
if
[[
$QUIET
-ne
1
]]
;
then
echo
"
$@
"
1>&2
;
fi
}
echoerr
()
{
if
[[
$
WAITFORIT_
QUIET
-ne
1
]]
;
then
echo
"
$@
"
1>&2
;
fi
}
usage
()
{
cat
<<
USAGE
>&2
Usage:
$cmdname
host:port [-s] [-t timeout] [-- command args]
$
WAITFORIT_
cmdname
host:port [-s] [-t timeout] [-- command args]
-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
...
...
@@ -24,47 +24,47 @@ USAGE
wait_for
()
{
if
[[
$TIMEOUT
-gt
0
]]
;
then
echoerr
"
$cmdname
: waiting
$TIMEOUT
seconds for
$
HOST
:
$
PORT
"
if
[[
$
WAITFORIT_
TIMEOUT
-gt
0
]]
;
then
echoerr
"
$
WAITFORIT_
cmdname
: waiting
$
WAITFORIT_
TIMEOUT
seconds for
$
WAITFORIT_HOST
:
$WAITFORIT_
PORT
"
else
echoerr
"
$cmdname
: waiting for
$
HOST
:
$
PORT
without a timeout"
echoerr
"
$
WAITFORIT_
cmdname
: waiting for
$
WAITFORIT_HOST
:
$WAITFORIT_
PORT
without a timeout"
fi
start_ts
=
$(
date
+%s
)
WAITFORIT_
start_ts
=
$(
date
+%s
)
while
:
do
if
[[
$ISBUSY
-eq
1
]]
;
then
nc
-z
$
HOST
$
PORT
result
=
$?
if
[[
$
WAITFORIT_
ISBUSY
-eq
1
]]
;
then
nc
-z
$
WAITFORIT_HOST
$WAITFORIT_
PORT
WAITFORIT_
result
=
$?
else
(
echo
>
/dev/tcp/
$
HOST
/
$
PORT
)
>
/dev/null 2>&1
result
=
$?
(
echo
>
/dev/tcp/
$
WAITFORIT_HOST
/
$WAITFORIT_
PORT
)
>
/dev/null 2>&1
WAITFORIT_
result
=
$?
fi
if
[[
$result
-eq
0
]]
;
then
end_ts
=
$(
date
+%s
)
echoerr
"
$
cmdname
:
$HOST
:
$PORT
is available after
$((
end_ts
-
start_ts
))
seconds"
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
$result
return
$
WAITFORIT_
result
}
wait_for_wrapper
()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if
[[
$QUIET
-eq
1
]]
;
then
timeout
$BUSYTIMEFLAG
$TIMEOUT
$0
--quiet
--child
--host
=
$HOST
--port
=
$PORT
--timeout
=
$TIMEOUT
&
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
$BUSYTIMEFLAG
$TIMEOUT
$0
--child
--host
=
$HOST
--port
=
$PORT
--timeout
=
$TIMEOUT
&
timeout
$
WAITFORIT_
BUSYTIMEFLAG
$
WAITFORIT_
TIMEOUT
$0
--child
--host
=
$
WAITFORIT_
HOST
--port
=
$
WAITFORIT_
PORT
--timeout
=
$
WAITFORIT_
TIMEOUT
&
fi
PID
=
$!
trap
"kill -INT -
$PID
"
INT
wait
$PID
RESULT
=
$?
if
[[
$RESULT
-ne
0
]]
;
then
echoerr
"
$cmdname
: timeout occurred after waiting
$TIMEOUT
seconds for
$
HOST
:
$
PORT
"
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
$RESULT
return
$
WAITFORIT_
RESULT
}
# process arguments
...
...
@@ -72,53 +72,53 @@ while [[ $# -gt 0 ]]
do
case
"
$1
"
in
*
:
*
)
hostport
=(
${
1
//
:/
}
)
HOST
=
${
hostport
[0]
}
PORT
=
${
hostport
[1]
}
WAITFORIT_
hostport
=(
${
1
//
:/
}
)
WAITFORIT_HOST
=
${
WAITFORIT_
hostport
[0]
}
WAITFORIT_PORT
=
${
WAITFORIT_
hostport
[1]
}
shift
1
;;
--child
)
CHILD
=
1
WAITFORIT_
CHILD
=
1
shift
1
;;
-q
|
--quiet
)
QUIET
=
1
WAITFORIT_
QUIET
=
1
shift
1
;;
-s
|
--strict
)
STRICT
=
1
WAITFORIT_
STRICT
=
1
shift
1
;;
-h
)
HOST
=
"
$2
"
if
[[
$HOST
==
""
]]
;
then
break
;
fi
WAITFORIT_
HOST
=
"
$2
"
if
[[
$
WAITFORIT_
HOST
==
""
]]
;
then
break
;
fi
shift
2
;;
--host
=
*
)
HOST
=
"
${
1
#*=
}
"
WAITFORIT_
HOST
=
"
${
1
#*=
}
"
shift
1
;;
-p
)
PORT
=
"
$2
"
if
[[
$PORT
==
""
]]
;
then
break
;
fi
WAITFORIT_
PORT
=
"
$2
"
if
[[
$
WAITFORIT_
PORT
==
""
]]
;
then
break
;
fi
shift
2
;;
--port
=
*
)
PORT
=
"
${
1
#*=
}
"
WAITFORIT_
PORT
=
"
${
1
#*=
}
"
shift
1
;;
-t
)
TIMEOUT
=
"
$2
"
if
[[
$TIMEOUT
==
""
]]
;
then
break
;
fi
WAITFORIT_
TIMEOUT
=
"
$2
"
if
[[
$
WAITFORIT_
TIMEOUT
==
""
]]
;
then
break
;
fi
shift
2
;;
--timeout
=
*
)
TIMEOUT
=
"
${
1
#*=
}
"
WAITFORIT_
TIMEOUT
=
"
${
1
#*=
}
"
shift
1
;;
--
)
shift
CLI
=(
"
$@
"
)
WAITFORIT_
CLI
=(
"
$@
"
)
break
;;
--help
)
...
...
@@ -131,47 +131,48 @@ do
esac
done
if
[[
"
$HOST
"
==
""
||
"
$PORT
"
==
""
]]
;
then
if
[[
"
$
WAITFORIT_
HOST
"
==
""
||
"
$
WAITFORIT_
PORT
"
==
""
]]
;
then
echoerr
"Error: you need to provide a host and port to test."
usage
fi
TIMEOUT
=
${
TIMEOUT
:-
15
}
STRICT
=
${
STRICT
:-
0
}
CHILD
=
${
CHILD
:-
0
}
QUIET
=
${
QUIET
:-
0
}
WAITFORIT_TIMEOUT
=
${
WAITFORIT_
TIMEOUT
:-
15
}
WAITFORIT_STRICT
=
${
WAITFORIT_
STRICT
:-
0
}
WAITFORIT_CHILD
=
${
WAITFORIT_
CHILD
:-
0
}
WAITFORIT_QUIET
=
${
WAITFORIT_
QUIET
:-
0
}
# check to see if timeout is from busybox?
TIMEOUT_PATH
=
$(
type
-p
timeout
)
TIMEOUT_PATH
=
$(
realpath
$TIMEOUT_PATH
2>/dev/null
||
readlink
-f
$TIMEOUT_PATH
)
if
[[
$TIMEOUT_PATH
=
~
"busybox"
]]
;
then
ISBUSY
=
1
BUSYTIMEFLAG
=
"-t"
WAITFORIT_TIMEOUT_PATH
=
$(
type
-p
timeout
)
WAITFORIT_TIMEOUT_PATH
=
$(
realpath
$WAITFORIT_TIMEOUT_PATH
2>/dev/null
||
readlink
-f
$WAITFORIT_TIMEOUT_PATH
)
if
[[
$WAITFORIT_TIMEOUT_PATH
=
~
"busybox"
]]
;
then
WAITFORIT_ISBUSY
=
1
WAITFORIT_BUSYTIMEFLAG
=
"-t"
else
ISBUSY
=
0
BUSYTIMEFLAG
=
""
WAITFORIT_
ISBUSY
=
0
WAITFORIT_
BUSYTIMEFLAG
=
""
fi
if
[[
$CHILD
-gt
0
]]
;
then
if
[[
$
WAITFORIT_
CHILD
-gt
0
]]
;
then
wait_for
RESULT
=
$?
exit
$RESULT
WAITFORIT_
RESULT
=
$?
exit
$
WAITFORIT_
RESULT
else
if
[[
$TIMEOUT
-gt
0
]]
;
then
if
[[
$
WAITFORIT_
TIMEOUT
-gt
0
]]
;
then
wait_for_wrapper
RESULT
=
$?
WAITFORIT_
RESULT
=
$?
else
wait_for
RESULT
=
$?
WAITFORIT_
RESULT
=
$?
fi
fi
if
[[
$CLI
!=
""
]]
;
then
if
[[
$RESULT
-ne
0
&&
$STRICT
-eq
1
]]
;
then
echoerr
"
$cmdname
: strict mode, refusing to execute subprocess"
exit
$RESULT
if
[[
$
WAITFORIT_
CLI
!=
""
]]
;
then
if
[[
$
WAITFORIT_
RESULT
-ne
0
&&
$
WAITFORIT_
STRICT
-eq
1
]]
;
then
echoerr
"
$
WAITFORIT_
cmdname
: strict mode, refusing to execute subprocess"
exit
$
WAITFORIT_
RESULT
fi
exec
"
${
CLI
[@]
}
"
exec
"
${
WAITFORIT_
CLI
[@]
}
"
else
exit
$RESULT
exit
$
WAITFORIT_
RESULT
fi
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment