markmcdermott.io (js, ruby, ...etc) by mark mcdermott

Clearing Ports

09/20/2024

estimated reading time:2 mins

I’ve probably looked this up and done it a million times over the years, but it never somehow stuck in my head. But a good way to find what is running on a port (say, port 3000) is lsof -i :3000 and that will give you the pid or process number running on that port. Then to stop that running process, you can do kill -9 <pid> where you replace <pid> with the actual process number.

Example:

 lsof -i :3000
COMMAND   PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    10192 mmcdermott   12u  IPv4 0x17ec2e84526c5d0b      0t0  TCP localhost:hbci (LISTEN)
ruby    10192 mmcdermott   13u  IPv6 0x17ec2e7f89b9dfbb      0t0  TCP localhost:hbci (LISTEN)
 kill -9 10192

A nice one liner for this is:

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

Some background:

Sources (accessed 9/20/24)