macos 在 Mac 上查找(并杀死)进程锁定端口 3000

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3855127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:15:02  来源:igfitidea点击:

Find (and kill) process locking port 3000 on Mac

macosprocess

提问by oma

How do I find (and kill) processes that listen to/use my tcp ports? I'm on mac os x.

如何找到(并杀死)侦听/使用我的 tcp 端口的进程?我在 mac os x 上。

Sometimes, after a crash or some bug, my rails app is locking port 3000. I can't find it using ps -ef...

有时,在崩溃或某些错误之后,我的 rails 应用程序锁定了端口 3000。我无法使用 ps -ef 找到它...

When doing

做的时候

rails server

I get

我得到

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

地址已被使用 - bind(2) (Errno::EADDRINUSE)

2014 update:

2014年更新:

To complete some of the answers below: After executing the kill commands, deleting the pid file might be necessary rm ~/mypath/myrailsapp/tmp/pids/server.pid

完成下面的一些答案:执行kill命令后,可能需要删除pid文件 rm ~/mypath/myrailsapp/tmp/pids/server.pid

回答by ghostdog74

  1. You can try netstat

    netstat -vanp tcp | grep 3000
    
  2. For macOS El Capitanand newer (or if your netstat doesn't support -p), use lsof

    sudo lsof -i tcp:3000 
    
  3. For Centos 7use

    netstat -vanp --tcp | grep 3000
    
  1. 你可以试试 netstat

    netstat -vanp tcp | grep 3000
    
  2. 对于macOS El Capitan和更新版本(或者如果您的 netstat 不支持-p),请使用lsof

    sudo lsof -i tcp:3000 
    
  3. 对于Centos下7使用

    netstat -vanp --tcp | grep 3000
    

回答by Filip Spiridonov

Find:

寻找:

sudo lsof -i :3000

Kill:

杀:

kill -9 <PID>

回答by Austin

Nothing above worked for me. Anyone else with my experience could try the following (worked for me):

以上没有对我有用。任何有我经验的人都可以尝试以下操作(对我有用):

Run:

跑:

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

then check status of the reported PID :

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

ps ax | grep <PID>

finally, "begone with it":

最后,“开始吧”:

kill -QUIT <PID>

回答by Zlemini

A one-liner to extract the PID of the process using port 3000 and kill it.

使用端口 3000 提取进程的 PID 并杀死它的单行程序。

lsof -ti:3000 | xargs kill

The -t flag removes everything but the PID from the lsof output, making it easy to kill it.

-t 标志从 lsof 输出中删除除 PID 之外的所有内容,从而很容易杀死它。

回答by Abhijith Sasikumar

Easiest solution:

最简单的解决方案

For single port:

对于单端口:

kill $(lsof -ti:3000)  #3000 is the port to be freed

Kill multiple ports with single line command:

使用单行命令杀死多个端口:

kill $(lsof -ti:3000,3001)  #here multiple ports 3000 and 3001 are the ports to be freed

lsof -ti:3000

lsof -ti:3000

82500 (Process ID/PID)

82500(进程 ID/PID)

lsof -ti:3001

lsof -ti:3001

82499

82499

lsof -ti:3001,3000

lsof -ti:3001,3000

82499 82500

82499 82500

kill $(lsof -ti:3001,3000)

杀死 $(lsof -ti:3001,3000)

Terminates both 82499 and 82500 processes in a single command.

在单个命令中终止 82499 和 82500 进程。

For using this in package.jsonscripts:

package.json脚本中使用它:

"scripts": { "start": "kill $(lsof -ti:3000,3001) && npm start" }

"scripts": { "start": "kill $(lsof -ti:3000,3001) && npm start" }

回答by Bruno Lemos

This single command line is easy to remember:

这个单一的命令行很容易记住:

npx kill-port 3000

npx kill-port 3000

For a more powerful tool with search:

对于更强大的搜索工具:

npx fkill-cli

npx fkill-cli



PS: They use third party javascript packages. npxcomes built in with Node.js.

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

Sources: tweet| github

资料来源: 推特| github

回答by DerMike

You can use lsof -i:3000.

您可以使用lsof -i:3000.

That is "List Open Files". This gives you a list of the processes and which files and ports they use.

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

回答by alexzg

In your .bash_profile, create a shortcut for terminatethe 3000 process:

在您的 中.bash_profile,为terminate3000 进程创建一个快捷方式:

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

Then, call $terminateif it's blocked.

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

回答by Tadele Ayelegn

To forcefully kill a process like that, use the following command

要强制终止这样的进程,请使用以下命令

lsof -n -i4TCP:3000 

Where 3000 is the port number the process is running at

其中 3000 是进程运行的端口号

this returns the process id(PID) and run

这将返回进程 id(PID) 并运行

kill -9 "PID"

Replace PID with the number you get after running the first command

用运行第一个命令后得到的数字替换 PID

For Instance, if I want kill the process running on port 8080

例如,如果我想终止在端口 8080 上运行的进程

回答by Kris

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

This will give you just the pid, tested on MacOS.

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