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

例如:localhost:8080


当前回答

如果你可以在Windows上使用PowerShell,你只需要:

Get-Process -Id (Get-NetTCPConnection -LocalPort "8080").OwningProcess | Stop-Process

其他回答

如果你想使用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

使用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
netstat -ano | findstr :PORT
kill PI

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

netstat -ano | findstr :<8080>

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

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

打开命令提示符并发出下面的命令

netstat -ano|findstr "PID :8888"

输出将显示占用端口的进程id

发出下面的命令杀死PID

taskkill /pid 8912 /f

您将收到如下输出

SUCCESS: The process with PID 8860 has been terminated.