Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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

 
discard

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.