如何删除已分配给端口的当前进程/应用程序?
例如: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
其他回答
我使用GitBash和我错误如下时运行
taskkill //PID XXXX 错误:PID为7420的进程无法终止。原因:这 进程只能强制终止(使用/F选项)。
所以使用//F如下,工作良好
任务杀戮 //F //PID XXXX
你可以运行一个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用户,您可以使用CurrPorts工具轻松地杀死正在使用的端口:
下面是在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
如果你想使用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