[Utility Post] What’s Running on Port 8000? (And how to stop it)

tl;dr…

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

— — — — —

In your travels you may have come across a screen that looks like this:

What is on port 8000?

If no other obvious service is running that probably means you have something using port 8000 as a daemon or in the background. There are many ways to do this, but the command below has proven useful for finding out exactly what’s running on a particular port (and it works on barren AWS EC2 instances!):

$ lsof -i TCP:8000 | grep LISTEN

See the “further reading” section below for details, but the lsof command lists “open files” [hence the name] and the -i flag shows network connections. We pass TCP:8000 and then grep for records that have LISTEN on them (ie processes listening/ready on 8000)

We are most interested in the number in second column, the PID, because we can use that to kill the process.

[Make 100% certain that the PID you think you want to kill is indeed the one really do want to kill. This can be very destructive!]

$ kill -9 PID_TO_KILL

As a somewhat clunky single-banger you can even run this (swap out $PORTNUM with your target port):

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

There are a lot of ways to do this (see here: http://stackoverflow.com/questions/3855127/find-and-kill-process-locking-port-3000-on-mac), but this quick-and-dirty approach might help save somebody some time.

Extra Credit: Example

If you want to work through an example of unlocking ports, see this repo or alternatively enter the following commands:

$ git clone https://github.com/valgaze/kill_port_example _tmpkill_port_example && cd _tmpkill_port_example && npm start

The above will clone the repo down to _tmpkill_port_example and then start a server on port 8000 as a daemon (see package.json)

Then in another terminal session in the same directory (_tmpkill_port_example) try running following command to see the “Error: listen EADDRINUSE :::8000” error:

$ npm run boot_server2

From here: use what you learned above to be able to boot server2 on port 8000 (which you can exit with CTRL-C)

Escape valve if you really break something:

$ npm run cleanup

Further reading

http://www.thegeekstuff.com/2012/08/lsof-command-examples