Finding which process is listening on a port

Josh Sherman
2 min read
Command-line Interface Shell Script

I’m known for having a lot of projects.

These days I have a day job, am trying to grind out more posts a week on this blog, regularly write for Alligator.io, run HolidayAPI and Ginpop and am currently on the “advisor track” for Startup School with CrowdSync.

This list doesn’t even include some other stealth projects (more to come ;) and juggling being a husband and father!!

All of that said, running a lot of projects means forgetting that I already have some code running locally that may conflict with another project.

These conflicts usually result in having one project’s code running in a Docker container in a detached, probably unnamed, screen session.

Then I go and try to docker-compose up another project and Docker starts to bark about how a port is in use by another process.

Wouldn’t it be nice if programs that try to listen to a power would tell you what process is already running on the port?

It would be nice, but it’s not the case. This usually results in my doing a web search for the 100th time on how to determine which process is listening on a port so that I can track it down and kill the process without having to sift through my active sessions.

Said web search usually leads me to a handful of articles that talk about how to do it on Windows with netstat which I don’t even have installed.

Then eventually I find the right post on how to do things in Linux and I find the PID and kill the process finally able to get back to work.

Turns out, this little problem doesn’t happen often enough for me to remember which search result is the right one.

Tired of this dance, the other day I took a solution that uses ss which my systems do have without any additional installation and turned it into a nice little re-usable shell alias.

For those that aren’t aware, ss is “another utility to investigate sockets”. It dumps socket statistics and is similar to netstat.

Using ss, I run the output through grep and cut to extract just the PID for the process that’s listening on the port. I can then take that and do a kill -9 on it and call it a day!

I called the shell function pp which stands for “PID Port”. Figured it would be easy to remember and paired up well with ss.

The shell function is as follows:

# PID Port
function pp {
  ss -lptn sport = :$1 | grep -Eo 'pid=[0-9]+' | cut -c5-
}

And to use it, simply type pp followed by the port number. For example:

pp 3306 # MySQL's port if you have MySQL running
Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles