How to terminate a process running on a particular port in Linux

Sometimes, we launch a service on Linux and get a message that another process already uses the same port. In such situations, a practical solution could involve terminating the process associated with the occupied port number. This will help free up system resources.

Terminate a process using its port number

We can terminate a process running on a particular port as follows:

  • List any process running on that particular port (for example, 8890):

lsof -i:8890
  • Terminate any process running on that particular port (for example, 8890):

kill $(lsof -i:8890 -t)

This command will terminate any process running on port 8890.

Note: By default, the kill command sends the soft signal, which will terminate the process or allow it to clean up before exiting.

We can send an immediate termination signal to the process as follows:

kill -9 $(lsof -i:8890 -t)
  • kill: Command to terminate the process

  • -9: Forcefully (immediate termination)

  • lsof: List related processes

  • -i: Specify the port number

  • -t: List process ID (PID) only

That's how we can terminate a process running on a particular port in Linux.

Example

Let's try to terminate a process running on a particular port in the provided terminal by following these steps:

  • Copy the given command kill -9 $(lsof -i:8890 -t).

  • Hover over the "Click to Connect..." space and click to make the session active.

  • Paste the copied command and press "Enter."

Note: Once the command is executed, the terminal will promptly revert to its original state. This behavior indicates the correct functionality of the command, as our terminal session operates on port 8890.

Terminal 1
Terminal
Loading...
Copyright ©2024 Educative, Inc. All rights reserved