如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
当前回答
如果您已经知道端口号,那么向进程发送软件终止信号(SIGTERM)可能就足够了:
kill $(lsof -t -i :PORT_NUMBER)
其他回答
下面是在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
简单的CMD工作我。容易记住
找到你想杀死的端口号并运行下面的CMD
npx kill-port 8080
完成端口后停止并得到此消息
npx: installed 3 in 13.796s
Process on port 8080 killed
我知道这是一个很老的问题,但发现很容易记住,快速命令杀死使用端口的应用程序。
要求:npm@5.2.0^版本
npx kill-port 8080
你也可以在这里阅读更多关于kill-port的内容:https://www.npmjs.com/package/kill-port
如果你想使用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
netstat -ano | findstr :PORT
kill PI