skip to Main Content

This topic is mentioned multiple times everywhere but none of the solutions work for me.

I have multiple Linux host machines, they are actually docker containers, based on alpine image. I would like to open a port on host-a permanently in a background process once the host (container) is up and ready. This signal informs other machines that the host-a is ready to do something.

Parallelly host-b and host-c are checking in bash loops that remote port and waiting for this signal, then continuing.

I use nc command to open the port permanently in the background and I use nc to check whether the remote port is opened or not. But unfortunately, my solution does not work and if it works then it is unstable, and exists after the first check.

host-a: open the port permanently in the background

nc -lk -v -p "$UP_SIGNAL_PORT" &
next-command-in-my-script...

This is how host-b and host-c check the port status:

while ! nc -w 5 -z "$domain" "$UP_SIGNAL_PORT" 2>/dev/null; do
  sleep 0.5
done
continue...

It seems that the command that opens the port exists after the first "ping".

I have also tried the following command but it behaves strangely:

[host-a]# nc -lk -p 12345 &
[host-a]# ps axu
1114 root      0:00 nc -lk -p 12345

[host-b]# nc -zv hello.com 12345
hello.com (172.19.0.2:12345) open

try to check the port again
[host-b]# nc -zv hello.com 12345
NO result


[host-a]# ps axu
1114 root      0:00 nc -lk -p 12345
the process is still running BUT the port seems to be closed 

after the first remote "ping"

What is wrong here?

The nc version that I have:

# nc
BusyBox v1.35.0 (2022-11-19 10:13:10 UTC) multi-call binary.

— UPDATE —

As per your suggestion, I tried this, but unfortunately did not work:

[host-a]# yes hello | nc -lk -v -p 12345 >/dev/null &
[1] 1125
[[email protected]]# listening on [::]:12345 ...

[[email protected]]# connect to [::ffff:172.19.0.2]:12345 from host-b.hello.com.docker_default:40841 ([::ffff:172.19.0.3]:40841)
nc: too many output retries

[1]+  Exit 1                  yes hello | nc -lk -v -p 12345 > /dev/null

host-b:

[[email protected]]# nc -zv host-a.hello.com 12345
host-a.hello.com (172.19.0.2:12345) open
[[email protected]]# nc -zv host-a.hello.com 12345
no answer after the 1st ping

Unfortunately, this exists also after the first ping.

2

Answers


  1. The netcat you opened in the background wants to read something from its standard input to write to its client, but it is not connected to a file handle or a terminal, so it can’t.

    Try something silly like

    yes hello | nc -lk -v -p "$UP_SIGNAL_PORT" >/dev/null &
    

    which keeps on sending hello to the standard input of nc forever.

    The redirection > /dev/null simply discards whatever traffic it receives from any clients.

    Login or Signup to reply.
  2. Wrap it in a function and start it in background:

    fun(){
        nc -lk -v -p "$UP_SIGNAL_PORT"
        fun
    }
    
    fun &
    

    Or use xinetd

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search