Max*_*ams 9 mongrel ruby-on-rails mongrel-cluster
在我的本地机器上,我用mongrel运行rails.我有一些在启动时运行的东西,通过config/initializers中的文件,它puts用来告诉我它正在使用哪个数据库,用于发送电子邮件的内容以及其他一些信息.
当我在端口3000,3001和3002上运行一组mongrel时,我只想在端口3000上为mongrel做这个报告.所以,我需要将它包装在一个if块中,该块测试当前运行的mongrel是哪个端口使用.任何人都可以告诉我如何在我的代码中得到这个?
好吧,我正在回答我自己的问题,因为我在设置赏金后才弄清楚!
我可以使用 获取当前正在运行的进程的pidProcess.pid。然后我可以ps afx | grep mongrel这样做,这给了我这样的结果
pid port
| |
V V
10761 pts/1 S 0:20 | \_/usr/local/bin/ruby /path/to/mongrel_rails start -p 3000
10762 pts/1 S 0:18 | \_/usr/local/bin/ruby /path/to/mongrel_rails start -p 3001
10763 pts/1 S+ 0:23 | \_/usr/local/bin/ruby /path/to/mongrel_rails start -p 3002
Run Code Online (Sandbox Code Playgroud)
然后我可以 grep 获取 pid,并从匹配行中读取端口号,看看它是否为 3000。
所以,我的代码是
if `ps afx | grep mongrel_rails`.split("\n").detect{|line| line =~ /^#{Process.pid}.+\-p\s3000/}
#this is a mongrel running on port 3000 - do the extra stuff
....
end
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果有人可以告诉我如何直接获取正在运行的杂种的端口,而不需要通过ps afx,Process.pid我仍然会给你赏金:)
| 归档时间: |
|
| 查看次数: |
135 次 |
| 最近记录: |