如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
当前回答
我为此写了一个很小的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);
})
}
});
}
其他回答
我知道这是一个很老的问题,但发现很容易记住,快速命令杀死使用端口的应用程序。
要求: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
最简单的解决办法,也是我唯一记得的办法:
在Windows Powershell中
假设我们想停止端口8080上的进程
获取过程:
netstat -ano | findstr :8080
停止进程
stop-process 82932
在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
第一步(与KavinduWije所写的接受答案相同):
netstat -ano | findstr:yourPortNumber
第2步更改为:
tskill typeyourPIDhere
注意:taskkill在某些git bash终端中不起作用