I want to know how many people are connected to my server. Since I'm doing comet applications, this is important

closed as too broad by Stefan Lasiewski, Tom O'Connor Aug 26 '13 at 23:51

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    What kind of server? What OS? If it's a web server which one? Your choice of tags really don't help at all. – John Gardeniers May 13 '11 at 4:36
  • Bear in mind that HTTP does not commonly maintain connections. So, users will only connect temporarily to pull a web-page, then immediately disconnect. Simply looking at open connections will not give you the number of people viewing your website. – Chris S May 13 '11 at 12:37
up vote 21 down vote accepted

There are about a zillion ways to do this but:

netstat | grep http | wc -l

Keep it mind that http is a stateless protocol. Each line can represent one client opening multiple sockets to grab different files (css, images, etc) that will hang out for awhile in a timewait state.

  • 13
    grep -c saves a fork. :) – HampusLi May 13 '11 at 9:35

Below are some commands of netstat using which you can check the number of connections a server has.

To display all active Internet connections to the servers, only established connections are included.

netstat -na

To display only active Internet connections to the server at port 80 and sort the results, allow to recognize many connections coming from one IP

netstat -an | grep :80 | sort

To display the list of the all IP addresses involved instead of just count.

netstat -n -p | grep SYN_REC | sort -u

If your webserver is apache, you can also use the server-status page (after enabled it).

I know this is an old question, but I found it while looking for a tidy way to count all incoming connections to a given port. The trouble with a blanket search for a port is that you'll also pick up outgoing connections, skewing your numbers.

Eg:

tcp        0      0 10.224.38.158:8080          10.225.239.6:38194          ESTABLISHED
tcp        0      0 10.224.38.158:8080          10.225.239.6:56825          ESTABLISHED
tcp        0      0 10.224.38.158:51512         10.226.194.28:8080          ESTABLISHED
tcp        0      0 10.224.38.158:51960         10.226.194.28:8080          TIME_WAIT
tcp        0      0 10.224.38.158:46937         10.226.194.28:8080          ESTABLISHED
tcp        0      0 10.224.38.158:51331         10.226.194.28:8080          TIME_WAIT
tcp        0      0 10.224.38.158:8080          10.225.239.6:29634          ESTABLISHED

An indiscriminate grep -c or awk | wc -l would return "7," but there are actually only "3" inbound connections. Anyway, I haven't found a graceful way to do this, and my awk-fu's not so not, but I ultimately opted to netstat -an, use awk to print only the "local address" column, and count the instances of the port I'm looking for.

$ netstat -an | grep -c 8080 91

$ netstat -an | awk '{print $4}' | grep -c 8080 31

So, 31 connected sockets on port 8080. I hope that helps. If anyone knows a more concise way to do this, then please do share it.

  • 3
    Whenever you use awk and grep in quick succession, chances are that awk alone will do the job. netstat -an | awk '$5 ~ /:8080$/ { C++ } END { print C }' – 200_success Aug 26 '13 at 19:55

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