Thursday, August 23, 2007

How to find out which process is listening on a port

for example, we want to find out which process is running on port 8005
  • First, use the following command to find if the port is is use or not
    netstat -an |grep 8005


  • Then, use the following command to find which process is using the port
    fuser -n tcp 8005
    the return will be something like '8005/tcp: 9113'.
    it shows pid.


  • After that, we can find the full process information.
    ps -ef |grep 9113



  • In the end. I wrote a script to put all those steps together
    ps_port.sh


    #!/bin/sh
    status=`netstat -an |grep $1`
    if [ "$status" == "" ]; then
    echo "port $1 is free"
    exit
    fi
    ps -ef |grep `fuser -n tcp $1`

    1 comment:

    Anonymous said...

    On windows, you can use the PortReporter from MicroSoft

    http://www.microsoft.com/downloads/details.aspx?familyid=69BA779B-BAE9-4243-B9D6-63E62B4BCD2E&displaylang=en