Terminating Processes Using Ports in Ubuntu

Hello world.
This is my very first post so I shall start simple.

Terminate ports in Ubuntu

If you encountered an error message that says :
Port 8090 is already being used by another process

It can only mean one thing: two programs are trying to use the same communication channel (port) on your Ubuntu system. This guide will show you how to identify and safely terminate such processes.

Understanding Ports :
Think of ports as numbered doorways on your computer. Different programs use specific ports to receive or send information. When a program opens a port, it becomes its "exclusive space" for communication. Conflicts arise when two programs try to use the same port simultaneously.

Identify :
Before terminating any process, it's crucial to understand what is occupying the port. Tools like lsof (List Open Files) can help: sudo lsof -t -i:<port number> Replace <port number> with the port you want to investigate. This command will list details about the process using that port (e.g., process ID, program name).

Terminate :
  1. Look for the PID associated with the unwanted process in the lsof output.
  2. Send a graceful SIGTERM signal using:kill<PID>
  3. If the process doesn't respond to SIGTERM, terminate forcefully but with caution: sudo kill -9 <PID>

By understanding ports and using lsof to identify processes, you can safely free up occupied ports in Ubuntu. Always strive for graceful termination whenever possible and ensure you're terminating the intended process. Mistakes can lead to unintended consequences like data loss or system instability.

Seeking for a one-liner? Here you go:
sudo kill -9 $(sudo lsof -t -i:<port number>)

Replace port number to the PID you wish to terminate.