How to stop a Daemon Server in Rails?

前端 未结 15 565
灰色年华
灰色年华 2020-12-12 09:52

I am running my rails application using the following

  $script/server -d webrick 

on my Ubuntu system , above command run the webrick s

相关标签:
15条回答
  • 2020-12-12 10:35

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

    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.

    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 "CONT", pid
    

    (Hat tip: jackr)

    0 讨论(0)
  • 2020-12-12 10:35

    if kill process not works, then delete file server.pid from MyRailsApp/tmp/pids/

    0 讨论(0)
  • 2020-12-12 10:38

    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:

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

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

    0 讨论(0)
提交回复
热议问题