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
[root@host-a.hello.com]# listening on [::]:12345 ...
[root@host-a.hello.com]# 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:
[root@host-b.hello.com]# nc -zv host-a.hello.com 12345
host-a.hello.com (172.19.0.2:12345) open
[root@host-b.hello.com]# nc -zv host-a.hello.com 12345
no answer after the 1st ping
Unfortunately, this exists also after the first ping.