Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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?

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
until ssh -o ConnectTimeout=4 hostname; do sleep 1; echo Retrying...; done << would work but I am looking for something else (actually in my case its not ssh) – lzap May 11 '12 at 9:53
Ok so then use for example netcat: nc -z -w 4 <ip> <port>. Returns 0 if the connection was successful, 1 if not. – Oliver May 11 '12 at 9:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.