Linux stop process by port

Blog

Sometimes a program or an application stops responding or you are working on a terminal and want to stop a process. In such cases you need to terminate or kill that process. Linux provides a kill command that takes the PID or Process Id of the process and terminates it.

In most cases, the only way to identify the process to be stopped or getting its PID is by using the specific port on which it is running or listening.
Thus, first you must be able to find the process listening on a specific port and then kill it. This post will outline different commands to find and kill a process running on a port in linux.
Method 1 : Using lsof
lsof stands for List of Opened Files and with appropriate options or flags, it can be used to return the PID of a process on some given port. Once you get the PID, use kill command to stop that process.
Below example will terminate the process listening on port 3000,

# get the PID of the process
sudo lsof -i:3000

# use the PID from above command and stop the process
sudo kill PID

Some times the process does not terminate by simply using kill . Use -9 flag to force stop the process as

sudo kill -9 PID

Above two commands can be merged into a single command as

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

This will find the process running on 3000 port and kill it. Notice the -t option. lsof used with this option will only return the PID of the process and that is why, it could be chained with kill .

# use apt
sudo apt install lsof

# or use yum
sudo yum install lsof

Method 2 : Using fuser command
fuser command accepts the port number and protocol as its arguments and returns the details of the process running on that port for the protocol.
It has a -k flag which is used for killing a process. Thus, with this command you do not need to use or merge two commands as there is a built in option for this. Example,

sudo fuser -k 8080/tcp

This will kill the process running on port 8080 and listening on tcp.
If fuser is not already installed on your system, then install it using apt or yum as shown below

Читайте также:  Astra linux дополнительные репозитории

# use apt
sudo apt install psmisc

# or use yum
sudo yum install psmisc

Method 3 : Using netstat
netstat command can also fetch system processes. Its output when chained with grep command filters it as per the grep expression.
Thus, below command will fetch only those processes which contain 3000 or are running on port 3000.

sudo netstat -lp | grep 3000

Here -l and -p are the flags of netstat where -l finds only listening processes and -p also fetches the PID of the process.
Copy the PID of the process from the above command and terminate it with kill as

sudo kill -9 PID

If netstat is not already installed on your system, then install it using apt or yum as shown below

# use apt
sudo apt install net-tools

# or use yum
sudo apt install net-tools

Method 4 : Using ss command
ss command can also be used to fetch the details of a running process on linux. Its output when chained with grep and the required port, will show only the processes running on the given port as shown below.

sudo ss -ltp | grep 3000

Here -l flag will list only Listening processes, -t will show the processes that are running over tcp and -p is required to display the PID of the processes.
Copy the PID from the last command execution and use it to kill the required process as shown below.

sudo kill -9 PID

If you are logged in as a root user which has all the privileges, then you are not required to use sudo with any of the above commands.
Click the clap below if you liked the article.

Share if it’s worth .

Источник

How to Stop a Process on a Specific Port in Linux and Windows: A Complete Guide

Learn how to stop a process on a specific port in Linux and Windows using commands like kill, taskkill, and pkill. Follow our step-by-step guide to solve issues related to processes running on a specific port.

When a process is running on a specific port, it can cause issues that require it to be stopped. In this blog post, we will discuss the steps to stop a process on a specific port in both linux and windows operating systems. We will also discuss various commands such as kill, taskkill, and pkill that can be used to stop a process on a specific port.

How to stop a process running on a specific port in Linux

In Linux, the process identification number (PID) is used to stop a process running on a specific port. The following steps outline how to stop a process on a specific port in Linux:

This command will show you the name of the process running on the specific port along with its PID.

Читайте также:  Show ip route in linux

This command will terminate the process running on the specific port.

  1. If multiple processes are running on the same port, use the ps command to identify all the PIDs and then use the kill command followed by the PIDs to stop all the processes.

Kill process based on port number in Windows

In Windows, the netstat command is used to find the PID of the process running on a specific port. The following steps outline how to stop a process on a specific port in Windows:

  1. Use the netstat command to identify the PID of the process running on a specific port in Windows.

This command will show you the name of the process running on the specific port along with its PID.

This command will terminate the process running on the specific port.

  1. If multiple processes are running on the same port, use the tasklist command to identify all the PIDs and then use the taskkill command followed by the PIDs to stop all the processes.
tasklist | findstr /i "program" taskkill /IM .exe /F 

Terminate processes listening on a TCP or UDP port in Linux

The fuser command along with the -k (kill) option can be used to terminate processes listening on a tcp or udp port in Linux. The following steps outline how to terminate processes listening on a tcp or udp port in Linux:

sudo fuser -k /tcp sudo fuser -k /udp 

This command will terminate all the processes listening on the specific port.

  1. The fuser command can also be used to find the PIDs of the processes listening on a specific port.
sudo fuser /tcp sudo fuser /udp 

Identify process using a specific port in Windows

The netstat command can be used to display active network connections and their status in Windows. The following steps outline how to identify a process using a specific port in Windows:

This command will show you all the active network connections along with the PID of the process.

  1. Look for the port number in the netstat output and identify the corresponding PID.
  2. Once the PID is identified, use the taskkill command followed by the PID to stop the process.

This command will terminate the process running on the specific port.

Kill process by name in Linux

The pkill command can be used to kill processes based on their name in Linux. The following steps outline how to kill a process by name in linux:

This command will stop all processes with a particular name.

In conclusion, we have discussed how to stop a process on a specific port in both Linux and Windows operating systems. We have covered various commands such as kill, taskkill, and pkill that can be used to stop a process on a specific port. It is important to use these commands with caution and to gain administrative privileges before stopping any process. We hope this blog post has been helpful in solving issues related to processes running on a specific port.

Читайте также:  Linux mint экран загрузка

Источник

Kill Process Running on a Specific Port in Linux

Want to kill the processes running on specific ports? No need to know the process ID or name. You can terminate a process based on the port number it is using.

Killing a process in Linux usually involves using the process ID with the kill command. You can also use the process name with killall command.

This works when you know the process ID or name. But in some cases, you may not know it or you just don’t necessarily need to know it.

For example, if you want to kill the processes running on specific ports, you may not need to know the process ID or name at all. You can terminate a process based on the port number it is using.

Let me show you how to do that.

Terminating processes based on their port numbers

The fuser command combined with the -k (kill) option will end all associated processes that are listening on a TCP or UDP port. Simply provide the port number and type (TCP or UDP) in the fuser command.

For instance, to end a process on UDP port 81, use the fuser command as:

Killing processes on a specific port

Similarly, use the fuser command to terminate a process on TCP port 3306:

You can use the lsof command to verify that processes are no longer running on the target port.

The classic way of killing processes using certain ports

For those who would rather not use fuser , lsof may be used to determine which processes are using a certain port and then use this information with the kill command.

As an instance, to kill all the process running on TCP port 3306, use the below command to detect the process id:

And now kill it with its pid:

One thing to note here is that certain processes like mysqld and apache2 might restart after you have killed them using the above commands. Even if you use the killall command, they will still appear after some time.

In such cases, I advise you to use application specific commands to stop a service. For example, to kill the Apache process on Ubuntu, use the command:

sudo systemctl stop apache2

Killing processes in standar procedure

This will completely kill the particular process.

Wrapping Up

This article covered how to terminate a process running on a specific port. With the fuser command, you don’t need to know the process details. Otherwise, the classic method of getting the process ID associated with port first and then killing it works as well.

Источник

Оцените статью
Adblock
detector