How to Find and Kill a Process Running on Specific Port

A process in Linux is nothing but a program in execution. It’s a running instance of a program. Any command that you execute starts a process.” – DigitalOcean

How to Kill a Process

The Linux operating system considers every running process as another open file. Hence, we can use lsof command utility to list all the running processes to identify the process id (PID) of the process we want to kill. Once we have PID we can run the kill command to terminate process.

  1. Find a process to kill

    Execute the lsof (list open files) command to list a process on a given port. For example, we want to kill a process running on port 3000. So, to find its process id, run:

    lsof -t -i:3000
  2. Kill the process

    Copy the PID (process id) shown as a result of above. Execute the kill command passing PID as an argument

    sudo kill -9 [PID]

    Hint: Replace [PID] with the process id you yield in the lsof command run.
    Note: -9 is a [signal] parameter used to immediately terminate a process, without allowing it to clean up or save any data.

The Shorter Way

As an shortcut alternative to above two commands, you could run both commands in a single line. Like this:

sudo kill -9 $(sudo lsof -t -i:3000)

Conclusion

In this Frequently Asked Question, you have learned to kill a process running on a specific port on Linux Operating System.

Leave a Reply