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

例如:localhost:8080


当前回答

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

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

其他回答

用于命令行:

for /f "tokens=5" %a in ('netstat -aon ^| find ":8080" ^| find "LISTENING"') do taskkill /f /pid %a

用于蝙蝠档案:

for /f "tokens=5" %%a in ('netstat -aon ^| find ":8080" ^| find "LISTENING"') do taskkill /f /pid %%a

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

你可以运行一个bat文件:

@ECHO OFF                                                                              
FOR /F "tokens=5" %%T IN ('netstat -a -n -o ^| findstr "9797" ') DO (
SET /A ProcessId=%%T) &GOTO SkipLine                                                   
:SkipLine                                                                              
echo ProcessId to kill = %ProcessId%
taskkill /f /pid %ProcessId%
PAUSE

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

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

在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