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

Sri*_*.V. 75 ubuntu ruby-on-rails

我使用以下操作我的rails应用程序

  $script/server -d webrick 
Run Code Online (Sandbox Code Playgroud)

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

  $kill pid
Run Code Online (Sandbox Code Playgroud)

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

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

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

mag*_*num 82

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

lsof -i:3000

它也将返回pid

  • `kill $(lsof -i:3000 -t)` - ** - t**选项代表**terse**,这意味着它只输出进程ID. (32认同)
  • 真棒; 这里是我的bash_aliases`别名stopRails ='kill -9 $(lsof -i:3000 -t)'`带有别名startRails ='rails server -d'`(如果你不在你的应用程序目录中会失败,但那是好的)最后`alias restartRails ='stopRails && startRails'` (6认同)
  • 即使在Mac/unix/bash上......是的.这非常有用.`lsof -i:3000`告诉我正在运行的红宝石的PID (2认同)

Dou*_*yer 36

像瑞安说:

你想要的pid在tmp/pids /

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

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

  • 在'lsof -i:3000`之后我获得红宝石的PID,然后`kill -9 1406orWhateverThePIDofRubyWas` - easypeasy - 谢谢! (5认同)

pgu*_*rio 32

耙子任务怎么样?

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 Code Online (Sandbox Code Playgroud)

用耙子停止或sudo耙子停止

  • 仅在守护程序挂起时才使用`kill -9`.否则,您将丢失Active Directory缓存中未刷新的数据. (9认同)
  • 这可能是,但你应该只在开发中运行像这样的服务器. (2认同)

Rya*_*yan 18

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

  • 这是一个可以分配给〜/ .bashrc文件中别名的单行:`kill -9 $(lsof -i:3000)&>/dev/null`."&>"之后的部分是可选的 - 它只是抑制了kill命令的一些输出. (7认同)

Now*_*ker 17

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

kill -INT $(cat tmp/pids/server.pid)
Run Code Online (Sandbox Code Playgroud)

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

kill $(cat tmp/pids/server.pid)
Run Code Online (Sandbox Code Playgroud)

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


Ram*_*aja 14

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

$ lsof -wni tcp:3000
Run Code Online (Sandbox Code Playgroud)

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

$ kill -9 <PID>
Run Code Online (Sandbox Code Playgroud)


Jus*_*ᚄᚒᚔ 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
Run Code Online (Sandbox Code Playgroud)

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

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

Process.kill "CONT", pid
Run Code Online (Sandbox Code Playgroud)

(帽子提示:jackr)


Tai*_*aiz 6

运行此命令:

locate tmp/pids/server.pid
Run Code Online (Sandbox Code Playgroud)

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

然后运行以下命令:

rm -rf [complete path of tmp/pids/server.pid file]
Run Code Online (Sandbox Code Playgroud)


jac*_*ckr 5

Ruby票证http://bugs.ruby-lang.org/issues/4777表明它是一个内核(Linux)错误.他们提供了一个解决方法(基本上相当于Ctrl-C/Ctrl-Z),如果你妖魔化服务器就可以使用:

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

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