Skip to content

Instantly share code, notes, and snippets.

@mpapis
Forked from anonymous/gist:1692424
Created January 28, 2012 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpapis/1692428 to your computer and use it in GitHub Desktop.
Save mpapis/1692428 to your computer and use it in GitHub Desktop.
Wait for a port in bash, should work on sysv linux
#!/usr/bin/env bash
# wait_port <port> <seconds>
check_port ()
{
netstat -tpln 2> /dev/null | sed -E '/^[^t]/ d; s/^([^ ]+ +){3}//; s/ .*$//; s/^.*://;' | grep --color "^$1$" > /dev/null
}
wait_port ()
{
typeset t
t=$2
while (( $t > 0 )) && ! check_port $1
do
echo "waiting for port $1 for $t secconds."
sleep 1s
(( t-- ))
done
check_port $1 && echo "Port $1 started." || {
echo "Port $1 was not started"
false
}
}
wait_port $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment