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

例如:localhost:8080


当前回答

下面是在WSL2中执行此操作的脚本

PIDS=$(cmd.exe /c netstat -ano | cmd.exe /c findstr :$1 | awk '{print $5}')
for pid in $PIDS
do
    cmd.exe /c taskkill /PID $pid /F
done

其他回答

在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

用于命令行:

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

我们可以通过简单地重新启动IIS来避免这种情况,使用下面的命令:

IISRESET

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

kill $(lsof -t -i :PORT_NUMBER)

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

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