DEV Community

Cover image for How to kill a Node.js process running at a specific port in Linux?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to kill a Node.js process running at a specific port in Linux?

Originally posted here!

To Kill a Node.js process running at a port, you can use the lsof (List of Open files) Linux command.

For our use case, Let's consider we want to kill a Node.js process at port 3000.

For that, we can use the -i flag in the lsof command and then pass the internet addresses we want to match. The -i flag selects the lists of files any of whose Internet address matches the address specified in it.

It can be done like this,

lsof -i tcp:3000

# Or
# lsof -i tcp:<YOUR_PORT_NUMBER>
Enter fullscreen mode Exit fullscreen mode

After that, it will show an output with all processes associated with the port number and its corresponding PID like this,

COMMAND   PID         USER
node    33092         johndoe
Enter fullscreen mode Exit fullscreen mode

Now all you have to do is get the PID number.

After that use the kill command and pass the -9 flag to SIGKILL the process (This will tell the OS to stop running the process) and then pass the PID of the process.

It can be done like this,

kill -9 33092
Enter fullscreen mode Exit fullscreen mode

Now you have successfully killed the Nodejs process running at PORT 3000. πŸš€

Feel free to share if you found this useful πŸ˜ƒ.


Top comments (0)