god 初体验 ( my first experience of god)
访问量: 3696
最近的一个项目需要用WEB 来控制 后台运行的进程。 搜索未果,于是在stackoverflow上发了个帖子(http://stackoverflow.com/questions/21069839/whats-the-best-practice-to-control-linux-process-from-web-page-rails)。 同时看到有人推荐godrb.com ,所以试了一下。
据说这个组建可以很好的控制进程的运行,加上各种监控。所以我试了试:
# /sg552/workspace/test_god/test.rb' 2 loop do 3 puts 'hi' 4 sleep 2 5 end # god script God.watch do |w| w.name = 'hi' w.start = 'ruby /sg552/workspace/test_god/test.rb' w.keepalive end # ouput on ubuntu sg552@youku:/sg552/workspace/test_god$ god -c hi.god -D I [2014-01-13 18:12:22] INFO: Loading hi.god I [2014-01-13 18:12:22] INFO: Syslog enabled. I [2014-01-13 18:12:22] INFO: Using pid file directory: /home/sg552/.god/pids I [2014-01-13 18:12:22] INFO: Socket already in use I [2014-01-13 18:12:22] INFO: Socket is stale, reopening I [2014-01-13 18:12:22] INFO: Started on drbunix:///tmp/god.17165.sock I [2014-01-13 18:12:22] INFO: hi move 'unmonitored' to 'up' I [2014-01-13 18:12:22] INFO: hi moved 'unmonitored' to 'up' I [2014-01-13 18:12:22] INFO: hi [trigger] process is not running (ProcessRunning) I [2014-01-13 18:12:22] INFO: hi move 'up' to 'start' I [2014-01-13 18:12:22] INFO: hi start: ruby /sg552/workspace/test_god/test.rb I [2014-01-13 18:12:22] INFO: hi moved 'up' to 'up'
god 的NB之处,在于它可以每5秒钟就检测一下对应的进程。 例如,我们看一下目前的进程情况:
# notice the pid of: 1875, sg552@youku:/sg552/workspace/test_god$ ps -ef | grep god sg552 1702 23782 0 18:20 pts/1 00:00:00 /home/sg552/.rbenv/versions/1.9.3-p327/bin/ruby /home/sg552/.rbenv/versions/1.9.3-p327/bin/god -c hi.god -D sg552 1875 1 0 18:23 ? 00:00:00 ruby /sg552/workspace/test_god/test.rb sg552 1885 1763 0 18:24 pts/2 00:00:00 grep --color=auto god
1875 就是god帮我们启动的test.rb 进程, 我们把它干掉试试。。。
$ kill -9 1875 # 这个时候, god的输出页面,就会这样, 很神奇吧?: I [2014-01-13 18:24:13] INFO: hi [trigger] process is not running (ProcessRunning) I [2014-01-13 18:24:13] INFO: hi move 'up' to 'start' I [2014-01-13 18:24:13] INFO: hi start: ruby /sg552/workspace/test_god/test.rb I [2014-01-13 18:24:13] INFO: hi moved 'up' to 'up' # 可以看到,它又被启动起来了。 sg552@youku:/sg552/workspace/test_god$ ps -ef | grep test.rb sg552 1376 23997 0 18:09 pts/4 00:00:00 vim test.rb sg552 1889 1 0 18:24 ? 00:00:00 ruby /sg552/workspace/test_god/test.rb sg552 1901 1763 0 18:27 pts/2 00:00:00 grep --color=auto test.rb
看看GOD是如何控制CPU和内存的使用量的:
# 这个脚本比较猛: test.rb 1 data = '' 2 loop do 3 puts 'hi' 4 100000.times { data << 'x'} 5 end # hi.god God.watch do |w| w.name = 'hi' w.start = 'ruby /sg552/workspace/test_god/test.rb' w.keepalive(:memory_max => 150.megabytes, :cpu_max => 50.percent ) end # 重启GOD, 之后,发现CPU越来越高的时候, GOD就会重启这个进程。 I [2014-01-13 18:48:33] INFO: hi [trigger] cpu out of bounds [9.345794392462356%%, *84.27876823360998%%, *91.39307897056767%%, *94.0134392181086%%] (CpuUsage) I [2014-01-13 18:48:33] INFO: hi move 'up' to 'restart' I [2014-01-13 18:48:33] INFO: hi stop: default lambda killer I [2014-01-13 18:48:33] INFO: hi sent SIGTERM I [2014-01-13 18:48:33] INFO: hi process stopped I [2014-01-13 18:48:33] INFO: hi start: ruby /sg552/workspace/test_god/test.rb I [2014-01-13 18:48:33] INFO: hi moved 'up' to 'up' # 干掉这个进程: $ god stop hi
一些其他的常见问题:
$ god stop hi # 停止 hi 这个任务(进程)
$ god quit # 彻底停止god
$ god status # 查看god的状态