Logo with initials

Find PID of process using port on macOS

There’s a server running as background process in your terminal. That server uses a port number. If you want to get that port number back, you have to destroy that process. That happens on the command line.

On macOS/OSX this command gives you the PID of the process using a specific port number:

lsof -t -i :3000
where 3000 is the port number.

And here’s how you destroy that process

kill -2 $(lsof -t -i :3000)

That’s it!

If you don’t want to remember it, and want to customise your terminal/shell, you can create aliases.

I’ve called mine fpfp, for “find pid for port” and kpfp for “kill process for port.” I use them like: fpfp 3000 and kpfp 3000 to target the process running on port 3000.

Create the aliases in your .zshrc like so

fpfp() {
    lsof -t -i :$1
}

fpfp(){
    kill -2 $(lsof -t -i :$1)
}