macos how - Find(and kill)process locking port 3000 on Mac




pid by (24)

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.

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


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:

ps -A

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:

ps -A | grep firefox

The most common way of passing signals to a program is with the kill command.

kill PID_of_target_process

lsof

List of all open files and the processes that opened them.

lsof -i -P | grep -i "listen"
kill -9 PID

or

 lsof -i tcp:3000 

You should try this code using the terminal:

$ killall -9 ruby

Execute in command line on OS-X El Captain:

kill -kill `lsof -t -i tcp:3000`

Terse option of lsof returns just the PID.


Find:

[sudo] lsof -i :3000

Kill:

kill -9 <PID>

In your .bash_profile, create a shortcut for terminate the 3000 process:

terminate(){
  lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9 
}

Then, call $terminate if it's blocked.


Add to ~/.bash_profile:

function killTcpListen () {
  kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
}

Then source ~/.bash_profile and run

killTcpListen 8080


Find and kill:

This single command line is easy and works correctly.

kill -9 $(lsof -ti tcp:3000)

To forcefully kill a process like that, use the following command

lsof -n -i4TCP:3000 

Where 3000 is the port number the process is running at

this returns the process id(PID) and run

kill -9 "PID"

Replace PID with the number you get after running the first command


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

Sources: tweet | github


I made a little function for this, add it to your rc file (.bashrc, .zshrc or whatever)

function kill-by-port {
  if [ "$1" != "" ]
  then
    kill -9 $(lsof -ni tcp:"$1" | awk 'FNR==2{print $2}')
  else
    echo "Missing argument! Usage: kill-by-port $PORT"
  fi
}

then you can just type kill-by-port 3000 to kill your rails server (substituting 3000 for whatever port it's running on)

failing that, you could always just type kill -9 $(cat tmp/pids/server.pid) from the rails root directory


Here's a helper bash function to kill multiple processes by name or port

fkill() {
  for i in [email protected];do export q=$i;if [[ $i == :* ]];then lsof -i$i|sed -n '1!p';
  else ps aux|grep -i $i|grep -v grep;fi|awk '{print $2}'|\
  xargs [email protected] sh -c 'kill -9 @&&printf "X %s->%s\n" $q @';done
}

Usage:

$ fkill [process name] [process port]

Example:

$ fkill someapp :8080 node :3333 :9000

To view the processes blocking the port:

netstat -vanp tcp | grep 3000

To Kill the processes blocking the port:

kill $(lsof -t -i :3000)


If nothing of the above works try:

lsof -n -i4TCP:3000

Try using Ctrl+C instead of Ctrl+Z to terminate previously running app.

Else you can find the process running on port 3000 by running [sudo] lsof -i :3000

Then kill the process kill -9 <PID>


you can use command

lsof -h

usage of this command to find port is

-i i   select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]

In your case enter

lsof -i :3000

you can perform this using node

install the module using

npm install freethenport -g

and on your terminal

node freethenport 3000

Step 1: Find server which are running: ps aux | grep puma Step 2: Kill those server Kill -9 [server number]


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>

Find the open connection

lsof -i -P | grep -i "listen"

Kill by process ID

kill -9 'PID'


  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
    

lsof -P | grep ':3000' | awk '{print $2}'

This will give you just the pid, tested on MacOS.


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.


Having tried several of these solutions, I have some notes:

DoubleCommand will not allow you to swap esc and caps-lock.

PCKeyboardHack will allow you to map capslock to escape, but does not have the capability to map escape to capslock. Recent versions will allow you to perform a complete swap by editing both keys.

This may or may not be sufficient for your needs (I know it is for mine).







macos process