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 use that :

sudo watch lsof -i -4 -a -P

and it returns a list.

how to get the count?

I tried this but doesn't work.

sudo watch lsof -i -4 -a -P | wc -l
share|improve this question

migrated from May 18 '12 at 22:55

This question came from our site for professional and enthusiast programmers.

This is more of a sys admin question. Voting to close. Expecting it will be migrated to ServerFault – Paul Sasik May 18 '12 at 1:02

I agree, this should probably be on ServerFault.

But, until then:

The issue with what you are doing is the watch command. Watch repeats a command so that you can see the output again and again over time.

My lsof doesn't accept a -4 argument, but if yours does, then

sudo lsof -i -4 -a -P | wc -l

Works for me.

share|improve this answer

try netstat -a | wc -l for all type of open sockets and netstat -l | wc -l for displaying server sockets.

share|improve this answer

To get the count, you need escape quotes to contain the entire command for the shells spawned by watch:

(Also, -4 didn't work here)

sudo watch "lsof -i -a -P | wc -l "
share|improve this answer

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.