简体   繁体   English

如何在Rails中停止守护进程服务器?

[英]How to stop a Daemon Server in Rails?

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 . 在我的Ubuntu系统上,上面的命令在后台运行webrick服务器。 I could kill the process using kill command 我可以使用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提供的启动服务器一样,谢谢。

EDIT When it is appropriate to start the daemon server ? 编辑什么时候适合启动守护程序服务器? Any real time scenario will help Thanks 任何实时场景都有助于谢谢

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

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)来关闭一个守护进程的服务器。

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 用耙子停止或sudo耙子停止

The process id of the daemon server is stored in your application directory tmp/pids/. 守护程序服务器的进程ID存储在应用程序目录tmp / pids /中。 You can use your standard kill process_id with the information you find there. 您可以将标准的kill process_id与您在那里找到的信息一起使用。

The only proper way 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 -9 if your daemon hung. 如果守护程序挂起,请使用kill -9 Remember the implications of kill -9 - if the data kept in Active Record caches weren't flushed to disk, you will lose your data. 记住kill -9的含义 - 如果Active Record高速缓存中保存的数据没有刷新到磁盘,则会丢失数据。 (As I recently did) (就像我最近做的那样)

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>

pguardiario beat me to it, though his implementation is a bit dangerous since it uses SIGKILL instead of the (recommended) SIGINT . pguardiario打败了我,虽然他的实现有点危险,因为它使用SIGKILL而不是(推荐) SIGINT Here's a rake task I tend to import into my development projects: 这是我倾向于导入到我的开发项目中的rake任务:

lib/tasks/stopserver.rake LIB /任务/ 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. 当且仅当pidfile存在时,这会向服务器发出中断。 It doesn't throw unsightly errors if the server isn't running, and it notifies you if it's actually shutting the server down. 如果服务器没有运行,它不会抛出难看的错误,它会通知你它是否实际关闭了服务器。

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: jackr ) (帽子提示: jackr

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]

A Ruby ticket, http://bugs.ruby-lang.org/issues/4777 , suggests it's a kernel (Linux) bug. Ruby票证http://bugs.ruby-lang.org/issues/4777表明它是一个内核(Linux)错误。 They give a work around (essentially equivalent to the Ctrl-C/Ctrl-Z one), for use if you've demonized the server: 他们提供了一个解决方法(基本上相当于Ctrl-C / Ctrl-Z),如果你妖魔化服务器就可以使用:

  1. kill -INT cat tmp/pids/server.pid kill -INT cat tmp/pids/server.pid
  2. kill -CONT cat tmp/pids/server.pid 杀死-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信号被处理,可能允许数据刷新等。

Here I leave a bash function which, if pasted in you .bashrc or .zshrc will 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 [ "$1" = "start" ]; then
     if [ "$2" = "" ]; then
        RENV="development"
     else
        RENV="$2"
     fi
     rails server -d -e "$RENV"
     return 0
  elif [ "$1" = "stop" ]; then
     if [ -f tmp/pids/server.pid ]; then
        kill $2 $(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 [ "$1" = "restart" ]; then
     rails stop && rails start $2
  else
     command rails $@
  fi;
}

More information in the blog post I wrote about it. 我写的关于它的博客文章中有更多信息。

i don't think it does if you use -d. 如果你使用-d,我不认为这样做。 I'd just kill the process. 我只是杀了这个过程。

In the future, just open up another terminal window instead and use the command without -d, it provides some really useful debugging output. 将来,只需打开另一个终端窗口并使用不带-d的命令,它提供了一些非常有用的调试输出。

If this is production, use something like passenger or thin, so that they're easy to stop the processes or restart the servers 如果这是生产,请使用乘客或瘦身,以便他们很容易停止进程或重新启动服务器

one-liner:  kill -INT `ps -e | grep ruby | awk '{print $1}'`

ps -e lists every process on the system ps -e列出系统上的每个进程
grep ruby searches that output for the ruby process grep ruby搜索ruby进程的输出
awk passes the first argument of that output (the pid) to kill -INT . awk将该输出的第一个参数(pid)传递给kill -INT


Try it with echo instead of kill if you just want to see the PID. 如果您只想查看PID,请尝试使用echo而不是kill。

如果kill进程不起作用,则从MyRailsApp / tmp / pids /删除文件server.pid

You can start your server in the background by adding -d to your command. 您可以通过在命令中添加-d来在后台启动服务器。 For instance: 例如:

puma -d

To stop it, just kill whatever process is running on port 3000: 要阻止它,只要杀死在端口3000上运行的任何进程:

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

I came here because I were trying to (unsuccesfully) stop with a normal kill, and thought I'd being doing something wrong. 我来到这里是因为我试图(不成功)停止正常的杀戮,并认为我做错了什么。

A kill -9 is the only sure way to stop a ruby on rails server? kill -9是在rails服务器上停止ruby的唯一可靠方法吗? What!? 什么!? Do you know the implications of this? 你知道这个含义吗? Can be a disaster... 可能是灾难......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM