How to Terminate a Process on a Specific Port in Ubuntu

Avatar

By squashlabs, Last Updated: Oct. 16, 2023

How to Terminate a Process on a Specific Port in Ubuntu

To terminate a process running on a specific port in Ubuntu, you can follow these steps:

Step 1: Identify the process running on the port

First, you need to identify the process ID (PID) of the process running on the specific port. You can do this by using the netstat command with the -tuln options to list all the network connections and their associated processes.

Open a terminal and run the following command:

netstat -tuln | grep <port>

Replace <port> with the port number you want to terminate the process on. This command will display the processes running on the specified port along with their PIDs.

Related Article: Exploring Do While Loop in Bash Scripting on Linux

Step 2: Terminate the process

Once you have identified the PID of the process running on the port, you can use the kill command to terminate it. The kill command sends a signal to the process, causing it to exit.

In the terminal, run the following command:

kill <PID>

Replace <PID> with the process ID you obtained from the previous step. This command will send the default SIGTERM signal to the process, which allows it to gracefully shut down. If the process does not respond to the SIGTERM signal, you can use the SIGKILL signal to forcefully terminate it by running the following command:

kill -9 <PID>

Again, replace <PID> with the process ID.

Alternative: Using lsof

Another way to identify and terminate a process on a specific port is by using the lsof command. The lsof command lists open files and the processes that have them open. This can be useful for finding processes associated with a particular port.

To list the processes running on a specific port, open a terminal and run the following command:

sudo lsof -i :<port>

Replace <port> with the port number you want to terminate the process on. This command will display the processes running on the specified port along with their PIDs.

To terminate a process using lsof, you can combine it with the kill command. For example, to terminate a process running on port 8080, you can run the following command:

sudo kill $(sudo lsof -t -i :8080)

This command uses command substitution to pass the output of lsof -t -i :8080 (which gives the PID of the process) as an argument to the kill command.

Best Practices

Here are some best practices to consider when terminating a process on a specific port:

1. Use the SIGTERM signal first: When terminating a process, it is generally recommended to use the SIGTERM signal first. This allows the process to perform any necessary cleanup tasks before exiting. If the process does not respond to the SIGTERM signal, then you can resort to using the SIGKILL signal.

2. Verify the process before terminating: Before terminating a process, it's a good practice to verify that it is indeed the process you want to terminate. You can check the process name and other details using the ps command, along with the PID obtained from netstat or lsof.

3. Use sudo when necessary: Some processes may require root privileges to be terminated. In such cases, you can use the sudo command to run the kill or lsof commands with elevated privileges.

4. Automate the process termination: If you frequently need to terminate processes on specific ports, you can consider automating the process using scripts or tools. For example, you can write a shell script that takes the port number as an argument and performs the necessary steps to terminate the process.

More Articles from the The Linux Guide: From Basics to Advanced Concepts series:

How to Set LD_LIBRARY_PATH in Linux

Setting the LD_LIBRARY_PATH environmental variable in Linux can be done in a few simple steps. This article provides a guide on how to set the LD_LIB… read more

Indentation Importance in Bash Scripts on Linux

Correct indentation is crucial in bash scripts on Linux. It plays a significant role in script readability, execution, and maintainability. This arti… read more

Locating Largest Memory in Bash Script on Linux

When working on Linux, it is important to be able to identify the largest memory in a bash script. This article will guide you through the process of… read more

How to Use Mkdir Only If a Directory Doesn't Exist in Linux

Creating a directory in Linux can be a simple task, but what if you only want to create it if it doesn't already exist? This article provides a step-… read more

Integrating a Bash Script into a Makefile in Linux

Integrating a Bash script into a Makefile in a Linux environment involves sourcing the script to leverage its functionality within the Makefile. This… read more

Tutorial on Traceroute in Linux

Traceroute is a powerful command in Linux systems that allows you to trace the path of network packets from your computer to a destination. This tuto… read more

How to Use Find and Locate on Linux

Using Find and Locate commands on Linux can greatly enhance your file searching capabilities. This tutorial provides an introduction to these command… read more

Running a Script within a Bash Script in Linux

Learn how to execute a script within a Bash script in a Linux environment. Discover the syntax for calling a script within a script and how to chain … read more

Executing Bash Scripts Without Permissions in Linux

This guide explores various methods to run bash scripts in a Linux system without requiring permissions. The article covers topics such as executing … read more

Executing Scripts in Linux Without Bash Command Line

Executing scripts in Linux without using the bash command line is a topic that software engineers often encounter. This article explores various alte… read more