How to Find Process IDs on ports with `lsof`
To kill a process running on a specific port use
lsof as so:
I want to tell you how to find the server process ID that is running a on a specific port. This is useful in a micro-service based development when you want to kill a server, not all of them.
I will apply this to a practical example of finding a server’s process ID and stopping the server.
You will understand a new Unix tool:
lsof and understand how to
incorporate it into your work-flow.
This article will take you about four minutes to read.
Ever have to stop a server process? It froze and you need to reset it, at no fault of your own?! :-)
ps -aef to list all the process names and associated
IDs. Put the output into
grep to find the server name, and stop it
using the command:
kill -9 <pid value from ps -aef>
Here’s how to kill the
jekyll web server:
What if you’re running a micro-service based application or multiple versions of the same application in parallel and needed to kill only one of them?
ps -aef would list process by their application name. If the process
had the same options, this would be the result:
Now, which process to stop? Stop them all and restart every other one? That might not be painful if the number of services is less than a handful.
n Gets Large, so does Pain
It does get painful when there are two handfuls or more of services or applications.
With micro-services, there’s a good chance the only thing you can differentiate between the services are the port number each service is running on.
Is there a tool that can list which process is running on each port?
Yes, there is!
The tool is:
lsof - short for: list open files. The specific option
needed is the
-i option, which lists open files that match an
Seems like overkill, right?
These are the reasons for this:
- Everything in UNIX is a file, running a micro-service requires running files.
- Micro-services are listening to ports, connecting files to ports.
lsof, we are essentially asking: what files are open on which
lsof by itself:
To find out what process is on port 4000 (the jekyll server):
And to find out the PID of the jekyll server running on port 4001:
To stop the server, just pick the one I want to stop based on the port.
Can I do better?
lsof is the right tool, using it with
grep does work. Is there a
better way to use it?
Yes there is.
Looking at the
lsof man page more, there’s an
specifically for IP addresses and ports.
-i [i] selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.
lsof -i instead:
Produces the same result as
lsof -i produces a
result faster as
lsof by processes for every port, which can be
over a 1000.
On Unix systems, Stopping servers can be as easy as using
grep to figure out which process ID to kill.
When there are more than one server that runs with the same name,
lsof -i will help you out more than
ps -aef, if you know the port