Helli,
I have a script that waits some time until a machine (being provisioned) comes up and then it connects to ssh and executes something. I use "sleep" for that.
I would like change my script to actively check the port in a bash loop and when it's ready (sshd is started and accepting connections) it would continue.
How to do that? Is there an UNIX command that is able to tell me if the TCP server port is responding?
Why don't you simply call
ssh
? It will wait until the remote host is available and time out after a while. If an error occurs, it will return 255, which means that you can retry it.As suggested in https://stackoverflow.com/a/50055449/15820811, you can use:
This tries to write data to port 22 (the SSH port) on
mysshserver.example.org
(replace with your server) every second until the data could be written successfully.