[macos] Find (and kill) process locking port 3000 on Mac

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

This question is related to macos process

The answer is


In mac OS

kill -9 $(lsof -i TCP:3000 | grep LISTEN | awk '{print $2}')


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

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


Find:

sudo lsof -i :3000

Kill:

kill -9 <PID>

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

For Instance, if I want kill the process running on port 8080


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>

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.


lsof -i tcp:port_number - will list the process running on that port

kill -9 PID - will kill the process

in your case, it will be

lsof -i tcp:3000 from your terminal find the PID of process

kill -9 PID


One of the ways to kill a process on a port is to use the python library: freeport (https://pypi.python.org/pypi/freeport/0.1.9) . Once installed, simply:

# install freeport
pip install freeport

# Once freeport is installed, use it as follows
$ freeport 3000
Port 3000 is free. Process 16130 killed successfully

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


Easiest solution:

kill $(lsof -ti:3000,3001,8080)

For single port:

kill $(lsof -ti:3000)

#3000 is the port to be freed

Kill multiple ports with single line command:

kill $(lsof -ti:3000,3001)

#here multiple ports 3000 and 3001 are the ports to be freed

lsof -ti:3000

82500 (Process ID)

lsof -ti:3001

82499

lsof -ti:3001,3000

82499 82500

kill $(lsof -ti:3001,3000)

Terminates both 82499 and 82500 processes in a single command.

For using this in package.json scripts:

"scripts": { "start": "kill $(lsof -ti:3000,3001) && npm start" }


Find the open connection

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

Kill by process ID

kill -9 'PID'


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.


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)

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 

TL;DR:

lsof -ti tcp:3000 -sTCP:LISTEN | xargs kill

If you're in a situation where there are both clients and servers using the port, e.g.:

$ lsof -i tcp:3000
COMMAND     PID         USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node       2043 benjiegillam   21u  IPv4 0xb1b4330c68e5ad61      0t0  TCP localhost:3000->localhost:52557 (ESTABLISHED)
node       2043 benjiegillam   22u  IPv4 0xb1b4330c8d393021      0t0  TCP localhost:3000->localhost:52344 (ESTABLISHED)
node       2043 benjiegillam   25u  IPv4 0xb1b4330c8eaf16c1      0t0  TCP localhost:3000 (LISTEN)
Google    99004 benjiegillam  125u  IPv4 0xb1b4330c8bb05021      0t0  TCP localhost:52557->localhost:3000 (ESTABLISHED)
Google    99004 benjiegillam  216u  IPv4 0xb1b4330c8e5ea6c1      0t0  TCP localhost:52344->localhost:3000 (ESTABLISHED)

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:

lsof -ti tcp:3000 -sTCP:LISTEN | xargs kill

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


Using sindresorhus's fkill tool, you can do this:

$ fkill :3000

These two commands will help you find and kill server process

  1. lsof -wni tcp:3000
  2. kill -9 pid

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 you want a code free way - open activity manager and force kill node :)


You can try this

netstat -vanp tcp | grep 3000

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

fkill() {
  for i in $@;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 -I@ sh -c 'kill -9 @&&printf "X %s->%s\n" $q @';done
}

Usage:

$ fkill [process name] [process port]

Example:

$ fkill someapp :8080 node :3333 :9000

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.


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.


Works for me for terminating node (Mac OS Catalina)

killall -9 node

I use this:

cat tmp/pids/server.pid | pbcopy

Then kill -9 'paste'


Execute in command line on OS-X El Captain:

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

Terse option of lsof returns just the PID.


You should try this code using the terminal:

$ killall -9 ruby

To kill multi ports.

$ npx kill-port 3000 8080 8081

Process on port 3000 killed
Process on port 8080 killed
Process on port 8081 killed

Hope this help!


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. npx comes built in with Node.js.

Sources: tweet | github