DEV Community

Juan Manuel Ramallo
Juan Manuel Ramallo

Posted on • Originally published at 1ma.dev

How to find the PID of a process using a specific port

To find out what processes are using a specific port, use lsof.

lsof -i :PORT
Enter fullscreen mode Exit fullscreen mode

Examples

List all processes using the 5432 port (commonly used by postgres)

$ lsof -i :5432
COMMAND  PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres 777    1ma    7u  IPv6 0x0123456789abcdef 0t0  TCP localhost:postgresql (LISTEN)
postgres 777    1ma    8u  IPv4 0x1123456789abcdef 0t0  TCP localhost:postgresql (LISTEN)
Enter fullscreen mode Exit fullscreen mode

List all processes using the 4000 port (this blog in development)

$ lsof -i :4000
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    59182    1ma   10u  IPv4 0x2123456789abcdef      0t0  TCP localhost:terabase (LISTEN)
Enter fullscreen mode Exit fullscreen mode

Alternatives

netstat can be used with grep to look for the port, but not possible in macOS as netstat won't output the process ID.

ps -ef can be used with grep too, but we'll need some bash sorcery to keep the first line of the ps output (the headers line).

Top comments (0)