如何删除已分配给端口的当前进程/应用程序?

例如:localhost:8080


当前回答

如果你想使用Python:检查是否可以在Python中杀死正在监听特定端口的进程,例如8080?

Smunk给出的答案很好。我在这里重复他的密码:

from psutil import process_iter
from signal import SIGTERM # or SIGKILL

for proc in process_iter():
    for conns in proc.connections(kind='inet'):
        if conns.laddr.port == 8080:
            proc.send_signal(SIGTERM) # or SIGKILL
            continue

其他回答

如果您已经知道端口号,那么向进程发送软件终止信号(SIGTERM)可能就足够了:

kill $(lsof -t -i :PORT_NUMBER)

在Windows PowerShell版本1或更高版本中停止端口3000上的进程:

Stop-Process (,(netstat -ano | findstr:3000).split() | foreach {$[$.length-1]}) -Force


根据@morganpdx的建议,这里有一个更像powershell的更好的版本:

Stop-Process -Id (get - netttcpconnection -LocalPort 3000)。OwningProcess force

使用GitBash的一行解决方案:

 tskill `netstat -ano | grep LISTENING | findstr :8080 | sed -r 's/(\s+[^\s]+){4}(.*)/\1/'`

将8080替换为服务器正在侦听的端口。

如果您需要经常使用它,请尝试添加到~/。Bashrc函数:

function killport() {
        tskill `netstat -ano | findstr LISTENING | findstr :$1 | sed -r 's/^(\s+[^\s]+){4}(\d*)$/\1/'`
}

然后简单地运行

killport 8080

以管理员身份运行cmd。然后在里面输入这个代码。

netstat -ano | findstr :<8080>

然后您可以看到PID在端口上运行。然后复制PID编号。(PID是帮助识别硬件产品或已注册软件产品的唯一数字。)并键入下面的代码行和按enter。

taskkill /PID <Enter the copied PID Number> /F

对于Windows用户,您可以使用CurrPorts工具轻松地杀死正在使用的端口: