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
A one-liner to extract the PID of the process using port 3000 and kill it.
The -t flag removes everything but the PID from the lsof output, making it easy to kill it.
Possible ways to achieve this:
top
The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top.
ps
The ps command lists running processes. The following command lists all processes running on your system:
You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:
The most common way of passing signals to a program is with the kill command.
lsof
List of all open files and the processes that opened them.
or
Find and kill:
This single command line is easy and works correctly.
You should try this, This technique is OS Independent.
In side your application there is a folder called tmp, inside that there is an another folder called pids. That file contains the server pid file. Simply delete that file. port automatically kill itself.
I think this is the easy way.
You should try this code using the terminal:
You can try
netstat
For macOS El Capitan and newer (or if your netstat doesn't support
-p
), uselsof
For Centos 7 use