Wednesday, November 25, 2015

How to find PID of process listening on a port in Linux? netstat and lsof command examples

http://javarevisited.blogspot.ca/2015/11/how-to-find-pid-of-process-listening-on-a-port-unix-netstat-lsof-command-examples.html

In Linux, many times, you want to find out the PID of a process which are listening on a port e.g. if multiple tomcat servers are running on a host then, how do you find the PID of the tomcat listening on port 8080? There are many UNIX commands to find the process using a specific port, but I'll share what I use. I always use the netstat command with -p option, which displays process id of the process listening on a port. Btw, netstat is not the only command to find all processes using a particular port, you can also use lsof command for the same purpose. If you remember, we have used lsof earlier to find all the processes accessing a file but it can also be used to find all processes accessing a specific port. You will see the example of both netstat and lsof commands in this article to find PID of process listening on a specific port in Linux.


Netstat command to find the PID of process listening on a port

So to find the PID of your Java server listening on port 8080, you can use following UNIX command:

$ netstat -nap | grep 8080
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 25414/java

here you go, 25414 is the PID of your tomcat server. Since tomcat is a Java web application it started with java command and that's why you see 25414/java.

Remember, if you are not logged in as root or the user under which your server is running, you might see following error:

No info could be read for "-p": geteuid()=XXX but you should be root

If you see this error, then just sudo as the user which is running the tomcat.

lsof command example to find the processes using a specific port

Here is the example of lsof command to list the process listening on a port.

$ lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
java 25414 appuser 44u IPv4 3733348 TCP *:XXX (LISTEN)

just remember to -i option and color (:) before the port i.e.  :8080. Btw, if you don't find lsof command in your PATH, search in /usr/sbin, more often /usr/sbin is not added into user's PATH. In that you can run the command as shown below:

$ /usr/sbin/lsof -i :8080
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
java 25414 appuser 44u IPv4 3733348 TCP *:XXX (LISTEN)

It will display the same result.

That's all about how to find the PID of the process listening on a port in UNIX or Linux. You can use both netstat and lsof command to get the PID, but I mostly use netstat because sometimes I don't remember the port but I know the PID. Since in the case of netstat I am just doing grep, even if give PID it will fetch the line from netstat output then I can get the port on which a particular process is listening. Anyway, I will show you a couple of more tricks to find the port on which a particular port is listening in next tutorial.

Here is the summary of how to find the process listening on a particular port in UNIX:

UNIX command to find the PID of the process listening on specific port



Related UNIX and Linux command tutorials for Java Programers:
  • 10 examples of find command in UNIX (examples)
  • How to call REST web service from UNIX command line? (command)
  • 10 examples of grep command in UNIX (examples)
  • Difference between soft link and hard link in Linux? (answer)
  • 10 examples of date command in Linux (examples)
  • How to get IP address from hostname and vice-versa in Linux (command)
  • 10 examples of tar command in UNIX (examples)
  • How to delete empty files and directory in UNIX (solution)
  • 10 examples of Vim in UNIX (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of sort command in Linux (examples)
  • How to make directory tree in one command? (example)
  • 10 examples of chmod command in UNIX (examples)
  • UNIX command to find out how long a process is running? (answer)
  • 5 examples of kill command in Linux (examples)
  • How to how long argument of a process in Solaris (command)
  • 10 examples of xargs command in Linux (examples)
  • UNIX command to find the size of file and directory? (command)
  • 10 tips to work fast in UNIX? (tips)
Further Reading

No comments:

Post a Comment