如何查找(并终止)侦听/使用TCP端口的进程?我在macOS上。

有时,在崩溃或一些错误之后,我的Rails应用程序会锁定端口3000。我用ps-ef找不到它。。。

运行时

rails server

我明白了

Address already in use - bind(2) (Errno::EADDRINUSE)

停止Node.js进程时也会出现同样的问题。即使在进程停止且应用程序停止运行后,端口3000仍被锁定。再次启动应用程序时

Address already in use (Errno::EADDRINUSE)

当前回答

在.bash_profile中,创建终止3000进程的快捷方式:

terminate(){
  lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9 
}

然后,如果被阻止,调用$terminate。

其他回答

在mac OS中

kill-9$(lsof-i TCP:3000|grep LISTEN|awk“{print$2}”)

这个命令行很容易记住:

npx压井口3000

您还可以一次关闭多个端口:

npx压井口3000 3001 3002

要获得更强大的搜索工具:

npx fkill客户端


PS:他们使用第三方javascript包。npx内置Node.js。

来源:推特| github

杀死多个端口。

$ npx kill-port 3000 8080 8081

Process on port 3000 killed
Process on port 8080 killed
Process on port 8081 killed

希望这有帮助!

添加到~/.bash_profile:

function killTcpListen () {
  kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
}

然后source~/.bash_profile并运行

killTcp侦听8080

以上任何内容都不适用于我。任何其他有我经验的人都可以尝试以下方法(适用于我):

Run:

lsof -i :3000 (where 3000 is your current port in use)

然后检查报告的PID的状态:

ps ax | grep <PID>

最后,“走开”:

kill -QUIT <PID>