如何杀死在 Linux 中的特定端口上运行的进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11583562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:52:19  来源:igfitidea点击:

How to kill a process running on particular port in Linux?

linuxunixportkill-process

提问by veer7

I tried to close the tomcat using ./shutdown.shfrom tomcat /bindirectory. But found that the server was not closed properly. And thus I was unable to restart
My tomcat is running on port 8080.

我尝试使用./shutdown.shfrom tomcat/bin目录关闭tomcat 。但是发现服务器没有正常关闭。因此我无法重新启动
我的 tomcat 正在端口上运行8080

I want to kill the tomcat process running on 8080. I first want to have the list of processes running on a specific port (8080) in order to select which process to kill.

我想杀死正在运行的 tomcat 进程8080。我首先想要在特定端口 (8080) 上运行的进程列表,以便选择要杀死的进程。

采纳答案by veer7

Use the command

使用命令

 sudo netstat -plten |grep java

used grep javaas tomcatuses javaas their processes.

使用grep java作为tomcat用途java作为他们的流程。

It will show the list of processes with port number and process id

它将显示带有端口号和进程 ID 的进程列表

tcp6       0      0 :::8080                 :::*                    LISTEN      
1000       30070621    16085/java

the number before /javais a process id. Now use killcommand to kill the process

前面的数字/java是进程 ID。现在使用kill命令杀死进程

kill -9 16085

-9implies the process will be killed forcefully.

-9意味着该进程将被强行杀死。

回答by nudzo

This fuser 8080/tcpwill print you PID of process bound on that port.

fuser 8080/tcp将打印绑定在该端口上的进程的 PID。

And this fuser -k 8080/tcpwill kill that process.

fuser -k 8080/tcp将终止该过程。

Works on Linux only. More universal is use of lsof -i4(or 6 for IPv6).

仅适用于 Linux。更普遍的是使用lsof -i4(或 6 用于 IPv6)。

回答by Brian Peterson

This prints to stdout the process ids of everything running on <port_number>:

这将打印到 stdout 上运行的所有进程的进程 ID <port_number>

fuser -n tcp <port_number> 

It also prints some stuff to stderr, so:

它还会向 stderr 打印一些内容,因此:

fuser -n tcp <port_number> 2> /dev/null

We can then supply these process ids to the killcommand:

然后我们可以将这些进程 ID 提供给kill命令:

sudo kill $(fuser -n tcp <port_number> 2> /dev/null)

You could also put this in a function if you do it a lot:

如果你经常这样做,你也可以把它放在一个函数中:

function killport() {
    sudo kill $(fuser -n tcp  2> /dev/null)
}

回答by ben rhouma moez

Linux: You can use this command if you know the port :

Linux:如果您知道端口,则可以使用此命令:

netstat -plten | grep LISTEN | grep 8080

AIX:

艾克斯

netstat -Aan | grep LISTEN | grep 8080

You then take the first column (example: f100050000b05bb8) and run the following command:

然后获取第一列(例如:f100050000b05bb8)并运行以下命令:

rmsock f100050000b05bb8 tcpcb

kill process.

杀死进程。

回答by Sanjay s.

To know the pid of service running on particular port :

要知道在特定端口上运行的服务的 pid:

netstat -tulnap | grep :*port_num*

you will get the description of that process. Now use kill or kill -9 pid. Easily killed.

您将获得该过程的描述。现在使用 kill 或 kill -9 pid。轻松杀死。

e.g

例如

netstat -ap | grep :8080

netstat -ap | grep :8080

tcp6       0      0 :::8080       :::*    LISTEN      1880/java 

Now:

现在:

kill -9 1880

Remember to run all commands as root

请记住将所有命令作为 root

回答by Kop4lyf

You can use the lsof command. Let port number like here is 8090

您可以使用 lsof 命令。让这里的端口号是 8090

lsof -i:8090

This command returns a list of open processes on this port.

此命令返回此端口上打开的进程列表。

Something like…

就像是…

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 75782 eoin 5u IPv6 0x01c1c234 0t0 TCP localhost:8090 (LISTEN)

To free the port, kill the process using it(the process id is 75782)…

要释放端口,请使用它杀死进程(进程 ID 为 75782)...

kill -9 75782

This one worked for me. here is the link from the original post: link

这个对我有用。这是原始帖子的链接link

回答by Adil Abbasi

try like this,

试试这样

 sudo fuser -n tcp -k 8080

回答by Franck Dernoncourt

To list any process listening to the port 8080:

列出任何侦听端口 8080 的进程:

lsof -i:8080

To kill any process listening to the port 8080:

杀死任何监听端口 8080 的进程:

kill $(lsof -t -i:8080)

or more violently:

或更猛烈地:

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

(-9corresponds to the SIGKILL - terminate immediately/hard killsignal: see List of Kill Signalsand What is the purpose of the -9 option in the kill command?. If no signal is specified to kill, the TERM signal a.k.a. -15or soft killis sent, which sometimes isn't enough to kill a process.).

-9对应于SIGKILL - terminate immediately/hard kill信号:请参阅杀死信号列表kill 命令中 -9 选项的用途是什么?。如果没有指定信号kill,则 TERM 信号又名-15soft kill发送,有时不足以杀死一个过程。)。

回答by Nandhitha Ramaraj

  1. lsof -i tcp:8000This command lists the information about process running in port 8000

  2. kill -9 [PID]This command kills the process

  1. lsof -i tcp:8000该命令列出运行在 8000 端口的进程信息

  2. kill -9 [PID]此命令终止进程

回答by Chaklader

In Windows, it will be netstat -ano | grep "8080"and we get the following message TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 10076

在 Windows 中,它将是netstat -ano | grep "8080",我们收到以下消息TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 10076

WE can kill the PID using taskkill /F /PID 10076

我们可以使用杀死PID taskkill /F /PID 10076