-
-
Save mpapis/1692428 to your computer and use it in GitHub Desktop.
Wait for a port in bash, should work on sysv linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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