Linux

How to Kill a Process Running on a Port

Last Updated Monday, 21 December 2020

It is common in Node.js and other languages to run a script on a certain port. Since ports can come in and out of use, it's also common to get the following error:

javascript Copy
Error: listen EADDRINUSE: address already in use :::3000 at Server.setupListenHandle [as _listen2] (net.js:1318:16) at listenInCluster (net.js:1366:12) at Server.listen (net.js:1452:7) at Function.listen (/application.js:618:24) at file:///httpdocs/index.js:149:5 Emitted 'error' event on Server instance at: at emitErrorNT (net.js:1345:8) at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'EADDRINUSE', errno: -98, syscall: 'listen', address: '::', port: 3000 }

Fortunately this is quite easy to fix.

Linux and Mac

To solve this issue on linux, you first want to find out the process ID or PID currently running on the port (in our case :3000). To do that, you can use lsof. If you want a way to remember lsof, it is that it means 'list of'.

shell Copy
lsof -i :3000

This will return processes running on port :3000. The next step is to kill the processes on that port. Note the PID, and then run the following command, replacing [PID] with your PID

shell Copy
kill -15 [PID]

Why -15? -15 refers to the message your computer will send. You should try -15 the first time, as this will lead to an orderly shutdown of port 3000. If that doesn't work, then try:

shell Copy
kill -9 [PID]

Now there will be nothing running on port :3000