好的,所以我想在 Rails 中创建一个操作来重新启动自身。我做了一些搜索并发现:
http://snippets.dzone.com/posts/show/5002 http://snippets.dzone.com/posts/show/5002
其中建议有 2 个命令,一个用于停止,另一个用于重新启动。以下杀戮:
ps -a|grep "/usr/local/bin/ruby script/server"|grep -v "grep /usr"|cut -d " " -f1|xargs -n 1 kill -KILL $1
-HUP 信号不会为我重新启动,所以我尝试修改上面的命令(进行调整,以便该命令与我在 Ubuntu 下启动服务器的方式正常工作):
ps -eaf|grep "ruby script/server"|grep -v grep|cut -d " " -f3|xargs -n 1 kill -KILL $1;script/server
这在我的环境中运行良好,因此我尝试设置一个操作来执行它:
def restart
fork { exec "ps -eaf|grep \"ruby script/server\"|grep -v grep|cut -d \" \" -f3|xargs -n 1 kill -KILL $1;script/server" }
redirect_to "/server_maintenance"
end
该操作很好地终止了服务器,但实际上并没有启动服务器备份:
=> Booting Mongrel
=> Rails 2.3.2 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/tcphack.rb:12:in `initialize_without_backlog': Address already in use - bind(2) (Errno::EADDRINUSE)
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel/tcphack.rb:12:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:93:in `new'
from /usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:93:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb:10:in `new'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/mongrel.rb:10:in `run'
from /usr/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/server:3
我不太明白为什么当 Mongrel 似乎刚刚退出时该地址已经在使用中。
我发现这个问题:
如何在 Mongrel 下重新启动 Rails,而不停止和启动 Mongrel https://stackoverflow.com/questions/74218/how-do-you-restart-rails-under-mongrel-without-stopping-and-starting-mongrel
但这些信号不会导致我的环境中的重新启动,它们只是最终终止进程。
有人对可行的方法有什么想法吗?关于我的环境的一些说明:我从新版本的 RubyGems 和 Mongrel 安装了 Rails。我使用 script/server 来启动服务器,当然使用 Mongrel。我使用的是 Ubuntu Hardy Heron。