如何查找(并终止)侦听/使用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)

当前回答

lsof -P | grep ':3000' | awk '{print $2}'

这将为您提供在MacOS上测试的pid。

其他回答

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

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

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

在端口上终止进程的方法之一是使用python库:freeport(https://pypi.python.org/pypi/freeport/0.1.9) . 安装后,只需:

# install freeport
pip install freeport

# Once freeport is installed, use it as follows
$ freeport 3000
Port 3000 is free. Process 16130 killed successfully

只在终端上写入

sudo kill -9 $(lsof -i :3000 -t)

希望,这是工作。

你可以使用lsof-i:3000。

即“列出打开的文件”。这将为您提供进程列表以及它们使用的文件和端口。

使用sindresorhus的fkill工具,您可以执行以下操作:

$ fkill :3000