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

The following is working as expected. (do not want to use nmap) I need to use nc (or any other built-in centOS) command in shell script to check the port 6379 of a remote server. I want the script to exit quickly if no response received in less than 1 second. But it seems that nc will wait for too long before quitting with exit code of 1

How do I "quickly" check if the port is listening?

# time nc -z 1234

real    0m21.001s
user    0m0.000s
sys 0m0.000s

# echo $?

# time nc -z 6379
Connection to 6379 port [tcp/*] succeeded!

real    0m0.272s
user    0m0.000s
sys 0m0.008s

# echo $?
share|improve this question
up vote 3 down vote accepted

nc has -w option for tuning the timeout. Try

time nc -w 1 -z 1234
share|improve this answer
It's a shame it won't allow less than one second though ... – David Fraser Mar 7 '13 at 9:44

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.