Categories
macos node.js process

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

2537

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)

4

  • 39

    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 }

    Jan 17, 2014 at 18:39

  • @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, 2019 at 15:12

  • works after finding processes with netstat and killing the process with kill -9 command!

    – Gaurav

    Apr 8, 2021 at 15:39

  • 3

    @richardec kill -9 $(lsof -ti:3000) not working?

    Jan 18 at 9:49


4077

  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

     lsof -i tcp:3000 
    
  3. For Centos 7 use:

     netstat -vanp --tcp | grep 3000
    

15

  • 43

    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, 2013 at 18:40

  • 281

    The “terse” flag to lsof produces output suitable for piping to a subsequent kill: lsof -t -i tcp:1234 | xargs kill

    – Manav

    Jan 7, 2014 at 4:58

  • 14

    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

    Nov 11, 2015 at 13:51

  • 8

    Another tip is to add -P to the lsof command so that the raw port is visible in the output: lsof -P -i:3000

    Jul 22, 2016 at 2:06

  • 14

    This post is only half the answer

    – png

    Mar 24, 2020 at 17:10

2710

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.

15

  • 85

    Sometimes lsof -i :port will show nothing. try sudo lsof -i :port.

    – kilik52

    Jan 30, 2014 at 12:05

  • 56

    Recommend trying kill -15 <PID> before resorting to -9 for safety.

    Aug 16, 2015 at 23:55

  • 23

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

    Oct 12, 2016 at 5:57

  • 27

    I think this answer should say what -9 does.

    Jan 17, 2017 at 19:13

  • 13

    Please don’t just use kill -9 without thinking twice, and trying other signals first. It will cause a process to exit immediately, without cleaning up after itself, possibly leaving a mess behind or leaving databases in inconsistent state… Try a TERM (default for kill, no flag needed) or QUIT (kill -3 pid) first at least, and check what process you are dealing with before sending a KILL.

    – niels

    Jan 26, 2017 at 11:58

651

+500

Quick and easiest solution:

kill -9 $(lsof -ti:3000)

For multiple ports:

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

#3000 is the port to be freed

Kill multiple ports with single line command:

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

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

lsof -ti:3000

If the prot is occupied, thie above command will return something like this: 82500 (Process ID)

lsof -ti:3001

82499

lsof -ti:3001,3000

82499
82500

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

Terminates both 82499 and 82500 processes in a single command.

For using this in package.json scripts:

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

In terminal you can use:

npm run start

6

  • 3

    This should be the best solution. Does both find AND kill in one short, simple command 🙂

    – Dotl

    Jul 22, 2021 at 10:02

  • I get “kill: not enough arguments” when the port is free. Is it possible to condition it to work both when is taken and when is free?

    Oct 7, 2021 at 11:50

  • Try: $(lsof -ti:3000) && kill -9 $(lsof -ti:3000)

    Oct 8, 2021 at 6:23

  • 1

    Based on the answer here, i have created tap brew tap devasghar/portkill && brew install devasghar/portkill/homebrew-portkill then you can do portkill 3000 & for multiple ports portkill 3000,3001

    – devasghar

    Feb 9 at 20:23


  • I tried this oneliner but I got the following: kill: not enough arguments

    Feb 17 at 13:45