How do I find processes that listens to/uses 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... How do I find the stupid thing and kill it, brutally... ?
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
Execute in command line on OS-X El Captain:
Terse option of lsof returns just the PID.
Add to
~/.bash_profile
:Then
source ~/.bash_profile
and runkillTcpListen 8080
Find:
Kill:
Nothing above worked for me. Anyone else with my experience could try the following (worked for me):
Run:
then check status of the reported PID :
finally, "begone with it":
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.
TL;DR:
If you're in a situation where there are both clients and servers using the port, e.g.:
then you probably don't want to kill both.
In this situation you can use
-sTCP:LISTEN
to only show the pid of processes that are listening. Combining this with the-t
terse format you can automatically kill the process: