How do I find (and kill) processes that listen to/use my TCP ports? I'm on macOS.
Sometimes, after a crash or some bug, my Rails app is locking port 3000. I can't find it using ps -ef
...
When running
rails server
I get
Address already in use - bind(2) (Errno::EADDRINUSE)
The same issue happens when stopping Node.js process. Even after the process is stopped and the app stops running, port 3000
is locked. When starting the app again, getting
Address already in use (Errno::EADDRINUSE)
Run the command lsof -i : (make sure to insert your port number) to find out what is running on this port. Copy the Process ID (PID) from the Terminal output. Run the command kill -9 (make sure to insert your PID) to kill the process on port.
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. Save this answer.
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
Find:
sudo lsof -i :3000
Kill:
kill -9 <PID>
PLEASE NOTE: -9
kills the process immediately, and gives it no chance of cleaning up after itself. This may cause problems. Consider using -15
(TERM) or -3
(QUIT) for a softer termination which allows the process to clean up after itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With