How To Stop A Process Running On A Specific Port In Linux

Avatar

By squashlabs, Last Updated: Oct. 16, 2023

How To Stop A Process Running On A Specific Port In Linux

To stop a process running on a specific port in Linux, you can follow one of the following methods:

Method 1: Using the lsof command

1. Open a terminal window.

2. Run the following command to identify the process ID (PID) of the process running on the specific port:

   lsof -i :<port_number>

Replace <port_number> with the actual port number on which the process is running. For example, to find the process running on port 8080:

   lsof -i :8080

The output will display the process details along with the PID.

3. Once you have identified the PID, you can use the kill command to stop the process. Run the following command:

   kill <PID>

Replace <PID> with the actual process ID. For example, to stop the process with PID 12345:

   kill 12345

This will send a termination signal to the process running on the specific port, causing it to stop.

Related Article: Interactions between Bash Scripts and Linux Command History

Method 2: Using the fuser command

1. Open a terminal window.

2. Run the following command to identify the process ID (PID) of the process running on the specific port:

   fuser -n tcp <port_number>

Replace <port_number> with the actual port number on which the process is running. For example, to find the process running on port 8080:

   fuser -n tcp 8080

The output will display the PID of the process.

3. Once you have identified the PID, you can use the kill command to stop the process. Run the following command:

   kill <PID>

Replace <PID> with the actual process ID. For example, to stop the process with PID 12345:

   kill 12345

This will send a termination signal to the process running on the specific port, causing it to stop.

Additional Notes:

Related Article: Troubleshooting: Unable to Save Bash Scripts in Vi on Linux

- If the process does not stop after sending the termination signal, you can use the kill -9 command to forcefully kill the process. However, this should only be used as a last resort, as it does not allow the process to perform any cleanup operations.

- It is important to ensure that you have the necessary permissions to stop a process. If you are not running the commands as a privileged user, you may need to use sudo before the commands to run them with elevated privileges.

- If you are unable to identify the process running on a specific port using the above methods, it is possible that the process is running as a system service or daemon. In such cases, you can use the service management commands specific to your Linux distribution to stop the service. For example, on Ubuntu, you can use the systemctl command to stop a service:

  sudo systemctl stop <service_name>

Replace <service_name> with the name of the service you want to stop.

- It is generally a good practice to gracefully stop a process by sending the termination signal before resorting to forceful termination (kill -9). This allows the process to perform any necessary cleanup operations and exit gracefully.

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

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

Using SSH to Connect to a Remote Server in Linux

This article provides a tutorial on using SSH to connect to a remote server in Linux. It covers topics such as the basics of SSH, generating and usin… read more

Executing a Bash Script with Multivariables in Linux

Learn how to call a script in bash with multiple variables in Linux. This article covers topics such as passing multiple variables to a bash script, … read more

How to Check If a File Does Not Exist in Bash

Verifying the non-existence of a file in Bash on a Linux system is essential for managing files and scripts. This article provides a guide on how to … 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

Object-Oriented Bash Scripting in Linux: Quick Intro

Learn about object-oriented programming in Linux with a focus on bash scripting. This article explores the principles of object-oriented programming,… read more

How to Pass Parameters to Scripts in Bash

This excerpt provides a brief overview of an article that explores how parameters are passed to scripts in Linux's Bash. The article covers various t… read more

How to Use Grep Command in Linux Unix

Learn how to use the grep command in Linux Unix with this tutorial. From understanding the syntax and regular expressions to advanced techniques like… read more

Using Variables in If Statements in Bash Scripts

Using variables in if statements in bash scripts allows for dynamic decision-making within the script. This article explores the syntax, examples, be… read more

How to Compare Strings in Bash: A Simple Guide

This guide explains how to compare strings in Bash, a feature in Linux. It covers various methods, including using the equality operator, inequality … read more