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


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

  • 20
    A very neat solution to kill a process on ANY user-specified port can be found in @Kevin Suttle's answer below. Reproduced here for posterity: function killport() { lsof -i TCP:$1 | grep LISTEN | awk '{print $2}' | xargs kill -9 } – user456584 Jan 17 '14 at 18:39
  • 3
    Port 3000 is not 'highly rails development specific' - Meteor natively runs on 3000 as well, and I'm sure there's others. – gregorvand Dec 7 '15 at 13:44
  • @user456584 's comment above should be the accepted answer ^^^ That function worked to kill the many processes i had running on a port – Aneuway Jun 19 at 15:12

32 Answers 32

  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
  • 11
    Thank you! Your answer gave birth to my "death_to 'port'" script. (#!/usr/bin/ruby lsof -t -i tcp:#{ARGV.first} | xargs kill) – Sv1 Oct 2 '13 at 18:40
  • 162
    The "terse" flag to lsof produces output suitable for piping to a subsequent kill: lsof -t -i tcp:1234 | xargs kill – Manav Jan 7 '14 at 4:58
  • 2
    I have put this into my ~/.bash_profile: findandkill() { port=$(lsof -n -i4TCP:$1 | grep LISTEN | awk '{ print $2 }') kill -9 $port } alias killport=findandkill So now I just have to type killport 8080 and it saves me some seconds – Alfonso Pérez Nov 11 '15 at 13:51
  • 3
    Another tip: Make sure to try sudo before the lsof if you don't see any output – Abe Petrillo Jul 21 '16 at 22:02
  • 2
    Another tip is to add -P to the lsof command so that the raw port is visible in the output: lsof -P -i:3000 – Jason Axelson Jul 22 '16 at 2:06


[sudo] lsof -i :3000


kill -9 <PID>
  • 59
    Sometimes lsof -i :port will show nothing. try sudo lsof -i :port. – kilik52 Jan 30 '14 at 12:05
  • 19
    Recommend trying kill -15 <PID> before resorting to -9 for safety. – Jamon Holmgren Aug 16 '15 at 23:55
  • 7
    @Jamon Holmgren why? what do both do? and why is a kill <PID> not sufficient / dangerous / incomplete? – Michael Trouw Nov 24 '15 at 12:45
  • 8
    @MichaelTrouw almost a year later, but here's your answer. :-) unix.stackexchange.com/a/8918 TL;DR kill -15 gives the process a chance to clean up after itself. – Jamon Holmgren Oct 12 '16 at 5:57
  • 4
    I think this answer should say what -9 does. – Joseph Fraley Jan 17 '17 at 19:13

Nothing above worked for me. Anyone else with my experience could try the following (worked for me):


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>
  • 14
    This actually seems a better answer than the one give much later by Filip Spiridonov, which has 277 upvotes against your 9. Yours was 6 months earlier, and has the same information with a bit more explanation. There is no justice... – Floris Dec 30 '15 at 22:15

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.

  • 1
    Will also kill browser with localhost:3000 open – rlovtang May 22 '18 at 9:45
  • 11
    You can filter out "listening" ports with: lsof -ti:3000 -sTCP:LISTEN – Zlemini May 22 '18 at 21:58

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.

  • I'm on 10.5 lepard (updated Q). I don't know if that matters here, but lsof doesn't display ports. lsof -h (lsof 4.78) is too cryptic too me... – oma Oct 4 '10 at 12:58
  • 1
    Hmm. Don't know for sure on Leopard, but try (as root -- that's important, I think) lsof -i:3000. – Matt Gibson Oct 4 '10 at 13:19

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

  • Can you share details on installing npx using brew? I tried installing it on my Mac High Sierra, 10.13.3 and it won't work. – realPK Oct 11 '18 at 22:44
  • @realPK npx comes with npm which comes with node.js, so it's not a separated package. Just upgrade your node.js and your npm versions. – Bruno Lemos Oct 12 '18 at 8:40
  • I do Java mostly, haven't exposed myself to Node yet. I found a different way of killing service running on port. TY for responding. – realPK Oct 13 '18 at 22:58
  • The need for NodeJS and JavaScript, to kill something running on port 3000 probably rails... seems like too much overhead to me. adding a simple line to your .bashrc or .zshrc with an alias would solve it without the need for the internet. alias kill3000='lsof -ti:3000 | xargs kill' then you can do: kill3000 – Khalil Gharbaoui Jan 9 at 11:19
  • Best solution of this problem.. thanks – King Rayhan Apr 25 at 9:17

Easiest solution:

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)  #3000 and 3001 are the ports to be freed

lsof -ti:3000

82500 (Process ID)

lsof -ti:3001


lsof -ti:3001,3000

82499 82500

kill $(lsof -ti:3001,3000)

Terminates both 82499 and 82500 processes in a single command.

You can use this in package.json scripts as well:

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


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

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

Then, call $terminate if it's blocked.


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


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

  • 8
    on MAC kill all pids on port 3000: lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9 – mike clagg May 13 '13 at 21:18
  • 1
    can more than one process listen to the same port? – Kris May 15 '13 at 16:16
  • Our rails app spawns workers which are child processes, and I have to use this to kill orphaned workers – mike clagg May 15 '13 at 18:19
  • 1
    this also kills webbrowsers connecting to port – fjsj Apr 27 '15 at 15:06
  • 5
    Here is working one: lsof -n -iTCP:3407 -sTCP:LISTEN -n -l -P | grep 'LISTEN' | awk '{print $2}' | xargs kill -9 – bob May 23 '16 at 11:19

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
  • 7
    That's, by far, NOT the simplest way. The upvoted replies don't require you to download and install anything. – Greg Pasquariello May 25 '16 at 19:20
  • 4
    When the prerequisites are met this is so simple and easy to remember. We have a different definition of "simplest" and this answer is perfectly valid and appropriate. Maybe it's just missing the instructions to install freeport with pip. – Cyril Duchon-Doris Jul 19 '17 at 15:18
  • under the hood, freeport is just a wrapper that calls lsof -t -i:3000.. seems unnecessary. – Corey Goldberg Nov 16 '18 at 17:01
  • This solution is not the easiest, but it complies 100% with what the OP asked... So it is in deed valid AF – danielrvt Nov 21 '18 at 15:11

Execute in command line on OS-X El Captain:

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

Terse option of lsof returns just the 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)

  • This won't work on a Mac machine, returns the following: kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] It will, however, work in most linux distros – Milan Velebit Sep 19 '18 at 12:08
  • 1
    @MilanVelebit Actually it works perfectly in my Mac machine (Sierra). It works fine if your port 3000 is occupied. However if no processes is blocking the port, then you will get kill: not enough arguments error. – Henry Sep 19 '18 at 14:24
  • That's just weird, I've two Macs (both High Sierra tho), I remember running those commands on both of them (old habits) and I know for certain that they don't run. I've just tried it again on my machine, knowing that the port is occupied, same error. :/ – Milan Velebit Sep 19 '18 at 14:33
  • Did you get a valid PID on running netstat -vanp tcp | grep 3000, for your port that's occupied? – Henry Sep 19 '18 at 14:46
  • 1
    I tried it in both bash and zsh shell. Works fine for me. Not sure why it's not working for you. May be some thing to with High Sierra? I have no idea :/ – Henry Sep 19 '18 at 15:27

Find the open connection

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

Kill by process ID

kill -9 'PID'

  • Thanks, your solution it's the most easy to read – CrsCaballero Jun 12 at 22:01

Find PID and kill the process.

lsof -ti:3000 | xargs kill

Find and kill:

This single command line is easy and works correctly.

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

Possible ways to achieve this:





ps -A


ps -A | grep firefox


kill PID_of_target_process


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

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


 lsof -i tcp:3000 

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


These two commands will help you find and kill server process

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

Add to ~/.bash_profile:

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

Then source ~/.bash_profile and run

killTcpListen 8080


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

$ fkill :3000


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
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

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

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

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

  • I had to backslash \$PORT to get the message to display correctly. Otherwise, works great! – ashwood Mar 17 at 20:24

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.


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


$ fkill [process name] [process port]


$ fkill someapp :8080 node :3333 :9000

If you want a code free way - open activity manager and force kill node :)


Use the following command to kill the process on port 3000 kill $(lsof -t -i:3000)


You can try this

netstat -vanp tcp | grep 3000

I use this:

cat tmp/pids/server.pid | pbcopy

Then kill -9 'paste'


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

protected by Community Dec 18 '18 at 19:09


Would you like to answer one of these unanswered questions instead?
