How to wait for an open port with netcat?

2019-01-22 02:21发布

问题:

I'm trying to do a custom dockerfile with jenkins on it. I would to wait until port 8080 is open instead of doing an ugly 'sleep 60' with netcat but in not very confident with bash scripts and netcat.

Here is an example of what i'm trying to do:

#!/bin/bash

opened=0

while [ "$opened"  == "0" ]; do
  echo "Waiting jenkins to launch on 8080..."
  nc -vz localhost 8080
done

echo "Jenkins launched"

回答1:

You can't set netcat to wait until some port is open, so you have to add part for waiting before next check is made. Try this:

#!/bin/bash

echo "Waiting jenkins to launch on 8080..."

while ! nc -z localhost 8080; do   
  sleep 0.1 # wait for 1/10 of the second before check again
done

echo "Jenkins launched"


回答2:

I suggest the following one liners:

## netcat version:
timeout 22 sh -c 'until nc -z $0 $1; do sleep 1; done' stackoverflow.com 443

## pure bash version:
timeout 22 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 1; done' stackoverflow.com 443

Both commands exit as soon as connection is established, trying every second for up to 22 seconds.

Note that thanks to timeout command exit code is 0 when port is accessible otherwise 124 (if no connection established within given time).



回答3:

As suggested here, you could also do the following if you don't have nc installed but just bash and coreutils:

#!/bin/bash

echo "Waiting jenkins to launch on 8080..."

while ! timeout 1 bash -c "echo > /dev/tcp/localhost/8080"; do   
  sleep 1
done

echo "Jenkins launched"


回答4:

I have found this a common enough problem to write a utility to wait for a port to open, with an optional timeout:

# without timeout
wait-port localhost:8080

# timeout after a minute
wait-port -t 60000 localhost:8080

It's open source and available at github.com/dwmkerr/wait-port. Hopefully others will find it useful!



回答5:

To expand on user987339's answer, here's how to easily wait for a port in your terminal:

waitport function

Add this function to your ~/.bashrc setup file:

waitport() {
    while ! nc -z localhost $1 ; do sleep 1 ; done
}

Log out then back in to load ~/.bashrc. Then, run this command to verify that port 3000 has a server listening to it:

$ waitport 3000
Connection to localhost port 3000 [tcp/hbci] succeeded!

This has been validated on macOS. It might not work on Fedora/CentOS, as they lack the -z option for netcat.