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
You can try netstat
netstat -vanp tcp | grep 3000
For macOS El Capitan and newer (or if your netstat doesn't support -p
), use lsof
lsof -i tcp:3000
For Centos 7 use:
netstat -vanp --tcp | grep 3000
Thank you! Your answer gave birth to my "death_to 'port'" script. (#!/usr/bin/ruby
lsof -t -i tcp:#{ARGV.first} | xargs kill
)The "terse" flag to lsof produces output suitable for piping to a subsequent kill:
lsof -t -i tcp:1234 | xargs kill
I have put this into my
~/.bash_profile
:findandkill() { port=$(lsof -n -i4TCP:$1 | grep LISTEN | awk '{ print $2 }') kill -9 $port } alias killport=findandkill
So now I just have to typekillport 8080
and it saves me some secondsAnother tip is to add
-P
to thelsof
command so that the raw port is visible in the output:lsof -P -i:3000
This post is only half the answer