Sunday, July 25, 2021

How to find and kill a Process Listening on a port in Linux? netstat and lsof command examples

In Linux, many times, you want to find out the PID of a process that is listening on a port e.g. if multiple tomcat servers are running on a host and you have to kill that process, but in order to kill that process you need the process id, how do you find the PID of the tomcat listening on port 8080? There are many Linux 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 the 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 the 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 the PID of process listening on a specific port in Linux.

On a related note, it's very important for a Java developer to learn about the Linux operating system and commands. The more you know about the Linux Operating system and different shell e.g. bash, csh or ksh, the better you can support your Java application.

That's why it's important to join a comprehensive Linux course, which provides an excellent introduction to essential shell commands in Linux. It will not only help you to learn about fundamental of Linux OS but also all-important commands you need to work efficiently 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 the following Linux 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  or process id 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. If you know how Linux works, then you can solve this error pretty quickly but if you don't then you will have a hard time.





lsof command example to find the processes using a specific port

Here is the example of the 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 colon (:) before the port like: 8080.

Btw, if you don't find the 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.

Btw, if you like the lsof command and interested in learning its other application, you should check my post 10 ways to use the lsof command in Linux. You can also take a look at below diagram by Julia Evans which effectively summarise the various options of lsof command in Linux:



Summary

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

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


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 you just 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.


Related UNIX and Linux command tutorials for Java Programmers:
  • 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)
  • The difference between the soft link and hard link in Linux? (answer)
  • 10 examples of date command in Linux (examples)
  • How to get an IP address from a 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)

Thanks for reading this article so far. If you like this Linux tutorial and find the information useful please share with your friends and colleagues. If you have any questions or feedback then please drop a note.


1 comment :

Post a Comment