Ruby-on-rails 如何在 Rails 中停止守护进程?

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

How to stop a Daemon Server in Rails?

ruby-on-railsubuntu

提问by Srinivas M.V.

I am running my rails application using the following

我正在使用以下命令运行我的 rails 应用程序

  $script/server -d webrick 

on my Ubuntu system , above command run the webrick server in background . I could kill the process using kill command

在我的 Ubuntu 系统上,上面的命令在后台运行 webrick 服务器。我可以使用 kill 命令终止进程

  $kill pid

Does rails provide any command to stop the background running daemon server ?

rails 是否提供任何命令来停止后台运行的守护程序服务器?

like the one provided by rails to start the server , Thanks .

就像 rails 提供的启动服务器一样,谢谢。

EDITWhen it is appropriate to start the daemon server ? Any real time scenario will help Thanks

编辑什么时候启动守护程序服务器是合适的?任何实时场景都会有所帮助 谢谢

采纳答案by pguardiario

How about a rake task?

耙子任务怎么样?

desc 'stop rails'
task :stop do
    pid_file = 'tmp/pids/server.pid'
    pid = File.read(pid_file).to_i
    Process.kill 9, pid
    File.delete pid_file
end

run with rake stop or sudo rake stop

使用 rake stop 或 sudo rake stop 运行

回答by magnum

if it can be useful, on linux you can find which process is using a port (in this case 3000) you can use:

如果它有用,在 linux 上您可以找到哪个进程正在使用端口(在本例中为 3000),您可以使用:

lsof -i :3000

lsof -i :3000

it'll return the pid too

它也会返回pid

回答by Douglas Meyer

Like Ryan said:

就像瑞安说的:

the pid you want is in tmp/pids/

你想要的pid在tmp/pids/

probably server.pid is the file you want.

可能 server.pid 是您想要的文件。

You should be able to run kill -9 $(cat tmp/pids/server.pid)to bring down a daemonized server.

您应该能够运行kill -9 $(cat tmp/pids/server.pid)以关闭守护程序服务器。

回答by Ryan

The process id of the daemon server is stored in your application directory tmp/pids/. You can use your standard kill process_idwith the information you find there.

守护程序服务器的进程 ID 存储在您的应用程序目录 tmp/pids/ 中。您可以将您的标准kill process_id与您在那里找到的信息结合使用。

回答by Nowaker

The only properway to kill the Ruby on Rails default server (which is WEBrick) is:

杀死 Ruby on Rails 默认服务器(即 WEBrick)的唯一正确方法是:

kill -INT $(cat tmp/pids/server.pid)

If you are running Mongrel, this is sufficient:

如果您正在运行 Mongrel,这就足够了:

kill $(cat tmp/pids/server.pid)

Use kill -9if your daemon hung. Remember the implications of kill -9- if the data kept in Active Record caches weren't flushed to disk, you will lose your data. (As I recently did)

kill -9如果您的守护进程挂起,请使用。请记住以下含义kill -9- 如果保存在 Active Record 缓存中的数据没有刷新到磁盘,您将丢失数据。(就像我最近所做的那样)

回答by Ramiz Raja

In your terminal to find out the process id (PID):

在您的终端中找出进程 ID (PID):

$ lsof -wni tcp:3000

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

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

$ kill -9 <PID>

回答by Justin ??????

pguardiariobeat me to it, though his implementation is a bit dangerous since it uses SIGKILLinstead of the (recommended) SIGINT. Here's a rake task I tend to import into my development projects:

pguardiario击败了我,尽管他的实现有点危险,因为它使用SIGKILL而不是 (recommended) SIGINT。这是我倾向于导入到我的开发项目中的 rake 任务:

lib/tasks/stopserver.rake

lib/tasks/stopserver.rake

desc 'stop server'
task :stopserver do
  pid_file = 'tmp/pids/server.pid'
  if File.file?(pid_file)
    print "Shutting down WEBrick\n"
    pid = File.read(pid_file).to_i
    Process.kill "INT", pid
  end
  File.file?(pid_file) && File.delete(pid_file)
end

This issues an interrupt to the server if and only if the pidfile exists. It doesn't throw unsightly errors if the server isn't running, and it notifies you if it's actually shutting the server down.

当且仅当 pidfile 存在时,才会向服务器发出中断。如果服务器没有运行,它不会抛出难看的错误,并且它会通知您它是否真的关闭了服务器。

If you notice that the server doesn't want to shut down using this task, add the following line after the Process.kill "INT"line, and try to upgrade to a kernel that has this bug fixed.

如果您注意到服务器不想使用此任务关闭,请在该行之后添加以下Process.kill "INT"行,并尝试升级到已修复此错误的内核。

Process.kill "CONT", pid

(Hat tip: Hymanr)

(帽子提示:Hymanr

回答by Taimoor Changaiz

Run this command:

运行此命令:

locate tmp/pids/server.pid

output:Complete path of this file. Check your project directory name to find your concerned file if multiple files are shown in list.

输出:此文件的完整路径。如果列表中显示多个文件,请检查您的项目目录名称以找到您关注的文件。

Then run this command:

然后运行这个命令:

rm -rf [complete path of tmp/pids/server.pid file]

回答by Hymanr

A Ruby ticket, http://bugs.ruby-lang.org/issues/4777, suggests it's a kernel (Linux) bug. They give a work around (essentially equivalent to the Ctrl-C/Ctrl-Z one), for use if you've demonized the server:

Ruby 票证http://bugs.ruby-lang.org/issues/4777表明这是一个内核 (Linux) 错误。他们提供了一种解决方法(基本上等同于 Ctrl-C/Ctrl-Z),如果您已经妖魔化了服务器,请使用:

  1. kill -INT cat tmp/pids/server.pid
  2. kill -CONT cat tmp/pids/server.pid
  1. 杀-INT cat tmp/pids/server.pid
  2. 杀死 -CONT cat tmp/pids/server.pid

This seems to cause the original INT signal to be processed, possibly allowing data flush and so on.

这似乎导致原始 INT 信号被处理,可能允许数据刷新等。

回答by Waiting for Dev...

Here I leave a bash function which, if pasted in you .bashrcor .zshrcwill alloy you do things like:

在这里,我留下了一个 bash 函数,如果将其粘贴到您中.bashrc.zshrc将合金化,您将执行以下操作:

rails start # To start the server in development environment
rails start production # To start the server in production environment
rails stop # To stop the server
rails stop -9 # To stop the server sending -9 kill signal
rails restart # To restart the server in development environment
rails restart production # To restart the server in production environment
rails whatever # Will send the call to original rails command

Here it is the function:

这是函数:

function rails() {
  if [ "" = "start" ]; then
     if [ "" = "" ]; then
        RENV="development"
     else
        RENV=""
     fi
     rails server -d -e "$RENV"
     return 0
  elif [ "" = "stop" ]; then
     if [ -f tmp/pids/server.pid ]; then
        kill  $(cat tmp/pids/server.pid)
        return 0
     else
        echo "It seems there is no server running or you are not in a rails project root directory"
        return 1
     fi
  elif [ "" = "restart" ]; then
     rails stop && rails start 
  else
     command rails $@
  fi;
}

More information in the blog postI wrote about it.

我写的博客文章中有更多信息。