如何删除已分配给端口的当前进程/应用程序?

例如:localhost:8080


当前回答

netstat -ano | findstr :PORT
kill PI

其他回答

我在Windows上运行zookeeper,无法使用zookeeper-stop.sh停止在2181端口运行的zookeeper,所以尝试了这个双斜线“//”方法来kill任务。它工作

     1. netstat -ano | findstr :2181
       TCP    0.0.0.0:2181           0.0.0.0:0              LISTENING       8876
       TCP    [::]:2181              [::]:0                 LISTENING       8876

     2.taskkill //PID 8876 //F
       SUCCESS: The process with PID 8876 has been terminated.

以管理员身份运行cmd。然后在里面输入这个代码。

netstat -ano | findstr :<8080>

然后您可以看到PID在端口上运行。然后复制PID编号。(PID是帮助识别硬件产品或已注册软件产品的唯一数字。)并键入下面的代码行和按enter。

taskkill /PID <Enter the copied PID Number> /F

我为此写了一个很小的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 -vanp tcp | grep 8888

例子

tcp4     0      0    127.0.0.1.8888   *.*    LISTEN      131072 131072  76061    0
tcp46    0      0    *.8888           *.*    LISTEN      131072 131072  50523    0

第二步:找到你的pid并杀死他们

对我来说

sudo kill -9 76061 50523

如果你使用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