Skip to content

Instantly share code, notes, and snippets.

@kalekseev
Forked from zimbatm/wait_port.sh
Created September 2, 2021 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalekseev/5c09c449fedd27ba6c76bee43edf1a0e to your computer and use it in GitHub Desktop.
Save kalekseev/5c09c449fedd27ba6c76bee43edf1a0e to your computer and use it in GitHub Desktop.
# Small function that checks if a TCP port on localhost is open.
#
# Timeout after 10 attempts.
#
# Usage: wait_port <port>
wait_port() (
local i=0 port=$1
echo "waiting on port $port"
# Bash has a magic filesystem that can be used to do basic TCP.
#
# Use 127.0.0.1 instead of "localhost" to avoid resolution issues I observed
# inside of the container.
while ! exec 3<>"/dev/tcp/127.0.0.1/$port"; do
sleep 0.1
((i += 1))
if [[ $i == 10 ]]; then
echo "error: timeout"
return 1
fi
done
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment