如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
如何删除已分配给端口的当前进程/应用程序?
例如: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
其他回答
如果你使用powershell 7+,这对我有用。只需在$PROFILE文件中添加此函数。
function killport([parameter(mandatory)] [string] $uport){
if($upid = (Get-NetTCPConnection -LocalPort $uport -ErrorAction Ignore).OwningProcess){kill $upid}
}
然后简单地使用killport 8080
或者如果你只喜欢命令,你可以试试这个:
kill $(Get-NetTCPConnection -LocalPort 8761 -ErrorAction Ignore).OwningProcess
如果你想使用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
用于命令行:
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
下面是在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
打开命令提示符并发出下面的命令
netstat -ano|findstr "PID :8888"
输出将显示占用端口的进程id
发出下面的命令杀死PID
taskkill /pid 8912 /f
您将收到如下输出
SUCCESS: The process with PID 8860 has been terminated.