Ruby-on-rails Rails 服务器说端口已使用,如何终止该进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4473229/
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-09-02 23:55:14  来源:igfitidea点击:

Rails server says port already used, how to kill that process?

ruby-on-rails

提问by Blankman

I'm on a mac, doing:

我在 mac 上,正在做:

rails server

I get:

我得到:

2010-12-17 12:35:15] INFO  WEBrick 1.3.1
[2010-12-17 12:35:15] INFO  ruby 1.8.7 (2010-08-16) [i686-darwin10.4.0]
[2010-12-17 12:35:15] WARN  TCPServer Error: Address already in use - bind(2)
Exiting

I know I can start one on a new port, but I want to kill this process.

我知道我可以在一个新端口上启动一个,但我想终止这个进程。

回答by idlefingers

Assuming you're looking to kill whatever is on port 3000 (which is what webrick normally uses), type this in your terminal to find out the PID of the process:

假设您要终止端口 3000(这是 webrick 通常使用的端口)上的任何内容,请在终端中键入以下内容以找出进程的 PID:

$ lsof -wni tcp:3000

Then, use the number in the PID column to kill the process:

然后,使用 PID 列中的数字来终止进程:

$ kill -9 PID

回答by Bijan

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

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

回答by Shahzad Tariq

You need to get process id of program using tcp port 3000. To get process id

您需要使用 tcp 端口 3000 获取程序的进程 id。要获取进程 id

lsof -i tcp:3000 -t

And then using that process id, simply kill process using ubuntu kill command.

然后使用该进程 ID,只需使用 ubuntu kill 命令杀死进程。

kill -9 pid

Or just run below mentioned combine command. It will first fetch pid and then kill that process.

或者只是运行下面提到的组合命令。它将首先获取 pid,然后终止该进程。

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

回答by saneshark

For anyone stumbling across this question that is not on a Mac: assuming you know that your server is running on port 3000, you can do this in one shot by executing the following:

对于那些在 Mac 之外遇到这个问题的人:假设您知道您的服务器正在端口 3000 上运行,您可以通过执行以下操作一次性完成此操作:

fuser -k 3000/tcp

But as Toby has mentioned, the implementation of fuser in Mac OS is rather primitive and this command will not work on mac.

但是正如 Toby 所提到的,在 Mac OS 中 fuser 的实现是相当原始的,这个命令在 mac 上不起作用。

回答by devudilip

Some times there is a chance where rails server not closed properly. You can find process used by rails

有时,rails 服务器可能没有正确关闭。您可以找到 rails 使用的进程

ps aux | grep rails

ps辅助| 铁轨

Output will be like

输出会像

user     12609  9.8  0.5  66456 45480 pts/0    Sl+  21:06   0:02 /home/user/.rvm/rubies/ruby-2.2.0-preview1/bin/ruby bin/rails s

Here process_id 12609 is used by your rails server.

这里的 process_id 12609 被你的 rails 服务器使用。

You can kill it easily by command

您可以通过命令轻松杀死它

kill -9 12609

杀死 -9 12609

回答by Lorenzo Sinisi

All the answers above are really good but I needed a way to type as little as possible in the terminal so I created a gem for that. You can install the gem only once and run the command 'shutup' every time you wanna kill the Rails process (while being in the current folder).

上面的所有答案都非常好,但我需要一种在终端中输入尽可能少的方法,因此我为此创建了一个 gem。您只能安装 gem 一次,并在每次想要终止 Rails 进程时运行命令“shutup”(在当前文件夹中)。

gem install shutup

gem install shutup

then go in the current folder of your rails project and run

然后进入 Rails 项目的当前文件夹并运行

shutup# this will kill the Rails process currently running

shutup# 这将杀死当前正在运行的 Rails 进程

You can use the command 'shutup' every time you want

您可以随时使用命令“shutup”

DICLAIMER: I am the creator of this gem

DICLAIMER:我是这颗宝石的创造者

NOTE: if you are using rvm install the gem globally

注意:如果您使用 rvm,请全局安装 gem

rvm @global do gem install shutup

回答by Rocker

ps aux|grep rails use this command you can kill the server

ps aux|grep rails 使用这个命令可以杀死服务器

回答by Sarwan Kumar

By default, rails server uses port 3000.
So, you have 2 options to run rails server.
1. Either you can run the server on other port by defining custom port using the following command
rails s -p 3001
2. Or you can kill all the running ruby process by running following command
killall -9 ruby
then run rails server

默认情况下,rails 服务器使用端口 3000。
因此,您有 2 个选项来运行 rails 服务器。
1. 您可以通过使用以下命令定义自定义端口来在其他端口上运行服务器
rails s -p 3001
2. 或者您可以通过运行以下命令来终止所有正在运行的 ruby​​ 进程,
killall -9 ruby
然后运行rails server

回答by Anmol Chawla

One line solution:

一行解决方案:

kill -9 $(ps aux | grep 'rails s' | awk {'print'}); rails s

回答by InsanelyADHD

Type in:

输入:

man lsof

Then look for -w, -n, and -i

然后查找 -w、-n 和 -i

-i: internet stuff -n: makes it faster -w: toggles warnings

-i:互联网内容 -n:让它更快 -w:切换警告

There are WAY more details on the man pages

手册页上有更多详细信息