DEV Community

Juan Manuel Ramallo
Juan Manuel Ramallo

Posted on • Originally published at 1ma.dev

4 2

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).

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️