Advertisement
Advertisement


Find (and kill) process locking port 3000 on Mac


Question

How do I find (and kill) processes that listen to/use my tcp ports? I'm on mac os x.

Sometimes, after a crash or some bug, my rails app is locking port 3000. I can't find it using ps -ef...

When doing

rails server

I get

Address already in use - bind(2) (Errno::EADDRINUSE)

2014 update:

To complete some of the answers below: After executing the kill commands, deleting the pid file might be necessary rm ~/mypath/myrailsapp/tmp/pids/server.pid

2019/03/19
1
1920
3/19/2019 1:24:19 PM

Accepted Answer

  1. You can try netstat

    netstat -vanp tcp | grep 3000
    
  2. For macOS El Capitan and newer (or if your netstat doesn't support -p), use lsof

    sudo lsof -i tcp:3000 
    
  3. For Centos 7 use

    netstat -vanp --tcp | grep 3000
    
2018/07/18
3178
7/18/2018 2:56:42 PM


Nothing above worked for me. Anyone else with my experience could try the following (worked for me):

Run:

lsof -i :3000 (where 3000 is your current port in use)

then check status of the reported PID :

ps ax | grep <PID>

finally, "begone with it":

kill -QUIT <PID>
2016/10/25

A one-liner to extract the PID of the process using port 3000 and kill it.

lsof -ti:3000 | xargs kill

The -t flag removes everything but the PID from the lsof output, making it easy to kill it.

2016/08/05

Easiest solution:

For single port:

kill $(lsof -ti:3000)  #3000 is the port to be freed

Kill multiple ports with single line command:

kill $(lsof -ti:3000,3001)  #here multiple ports 3000 and 3001 are the ports to be freed

lsof -ti:3000

82500 (Process ID/PID)

lsof -ti:3001

82499

lsof -ti:3001,3000

82499 82500

kill $(lsof -ti:3001,3000)

Terminates both 82499 and 82500 processes in a single command.

For using this in package.json scripts:

"scripts": { "start": "kill $(lsof -ti:3000,3001) && npm start" }

2020/03/30

This single command line is easy to remember:

npx kill-port 3000

For a more powerful tool with search:

npx fkill-cli


PS: They use third party javascript packages. npx comes built in with Node.js.

Sources: tweet | github

2019/04/26

You can use lsof -i:3000.

That is "List Open Files". This gives you a list of the processes and which files and ports they use.

2013/07/01

Source: https://stackoverflow.com/questions/3855127
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]