如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
当前回答
我知道这是一个很老的问题,但发现很容易记住,快速命令杀死使用端口的应用程序。
要求:npm@5.2.0^版本
npx kill-port 8080
你也可以在这里阅读更多关于kill-port的内容:https://www.npmjs.com/package/kill-port
其他回答
使用GitBash的一行解决方案:
tskill `netstat -ano | grep LISTENING | findstr :8080 | sed -r 's/(\s+[^\s]+){4}(.*)/\1/'`
将8080替换为服务器正在侦听的端口。
如果您需要经常使用它,请尝试添加到~/。Bashrc函数:
function killport() {
tskill `netstat -ano | findstr LISTENING | findstr :$1 | sed -r 's/^(\s+[^\s]+){4}(\d*)$/\1/'`
}
然后简单地运行
killport 8080
如果你想使用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
我为此写了一个很小的node js脚本。就像这样运行它: node killPort.js 8080或任何你需要杀死的端口。保存如下文件到killPort.js文件中:
const { exec } = require('child_process');
const fs = require(`fs`);
const port = process.argv.length > 2 ? process.argv[2] : ``;
if (!port || isNaN(port)) console.log(`port is required as an argument and has to be a number`);
else {
exec(`netstat -ano | findstr :${port}`, (err, stdout, stderr) => {
if (!stdout) console.log(`nobody listens on port ${port}`);
else {
const res = stdout.split(`\n`).map(s => s.trim());
const pid = res.map(s => s.split(` `).pop()).filter(s => s).pop();
console.log(`Listener of ${port} is found, its pid is ${pid}, killing it...`);
exec(`taskkill /PID ${pid} /F`, (err, stdout, stderr) => {
if (!stdout) console.log(`we tried to kill it, but not sure about the result, please run me again`);
else console.log(stdout);
})
}
});
}
打开命令提示符并发出下面的命令
netstat -ano|findstr "PID :8888"
输出将显示占用端口的进程id
发出下面的命令杀死PID
taskkill /pid 8912 /f
您将收到如下输出
SUCCESS: The process with PID 8860 has been terminated.
如果你可以在Windows上使用PowerShell,你只需要:
Get-Process -Id (Get-NetTCPConnection -LocalPort "8080").OwningProcess | Stop-Process