如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
如何删除已分配给端口的当前进程/应用程序?
例如:localhost:8080
当前回答
netstat -ano | findstr :PORT
kill PI
其他回答
在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终端中不起作用
你可以运行一个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
我为此写了一个很小的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);
})
}
});
}
如果你使用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