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

时间:2009-07-22 09:24:07

标签: ruby-on-rails ubuntu

我正在使用以下

运行我的rails应用程序
  $script/server -d webrick 

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

杀死进程
  $kill pid

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

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

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

15 个答案:

答案 0 :(得分:81)

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

  

lsof -i:3000

它也将返回pid

答案 1 :(得分:36)

像瑞安一样说:

  

你想要的pid是在tmp / pids /

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

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

答案 2 :(得分:32)

rake任务怎么样?

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

以佣金止损或sudo佣金止损

运行

答案 3 :(得分:18)

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

答案 4 :(得分:16)

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

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

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

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

如果您的守护程序挂起,请使用kill -9。请记住kill -9的含义 - 如果Active Record缓存中保存的数据未刷新到磁盘,则会丢失数据。 (正如我最近所做的那样)

答案 5 :(得分:14)

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

$ lsof -wni tcp:3000

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

$ kill -9 <PID>

答案 6 :(得分:6)

pguardiario打败了我,虽然他的实施有点危险,因为它使用SIGKILL而不是(推荐)SIGINT。这是我倾向于导入我的开发项目的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

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

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

Process.kill "CONT", pid

(帽子提示:jackr

答案 7 :(得分:6)

运行此命令:

locate tmp/pids/server.pid

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

然后运行此命令:

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

答案 8 :(得分:5)

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
  3. 这似乎导致原始INT信号被处理,可能允许数据刷新等等。

答案 9 :(得分:4)

这里我留下了一个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

这是功能:

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;
}

我在blog post撰写的有关此内容的更多信息。

答案 10 :(得分:3)

如果使用-d,我认为不会这样做。我只是要杀了这个过程。

将来,只需打开另一个终端窗口并使用不带-d的命令,它提供了一些非常有用的调试输出。

如果这是生产,请使用乘客或瘦身之类的东西,这样他们就可以轻松停止流程或重启服务器

答案 11 :(得分:3)

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

ps -e列出系统上的每个进程
grep ruby搜索ruby进程的输出
awk传递该输出的第一个参数 (pid)到kill -INT

如果你只是想看PID,请用echo而不是kill来试试。

答案 12 :(得分:2)

如果kill进程不起作用,那么 从MyRailsApp / tmp / pids /

删除文件server.pid

答案 13 :(得分:1)

我来到这里是因为我试图(不成功地)以正常的杀戮停止,并且认为我做错了什么。

kill -9是在rails服务器上停止ruby的唯一可靠方法吗? 什么!?你知道这个含义吗?可能是灾难......

答案 14 :(得分:1)

您可以通过在命令中添加-d在后​​台启动服务器。例如:

puma -d

要停止它,只需杀死端口3000上正在运行的所有进程即可:

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