如何使用上帝来捕获过程输出?

时间:2018-11-01 03:40:01

标签: ruby god

试图获得一个简单的God演示工作。

在一个空目录中,根据God文档创建了以下文件:

simple.rb:

loop do
  puts 'Hello'
  sleep 1
end

simple.rb:

God.watch do |w|
  w.name = "simple"
  w.start = "ruby simple.rb"
  w.log = 'myprocess.log'
  w.keepalive
end

然后我运行:
$ sudo god -c simple.god -D

并获得以下输出:

I [2018-10-31 23:19:39]  INFO: Loading simple.god
I [2018-10-31 23:19:39]  INFO: Syslog enabled.
I [2018-10-31 23:19:39]  INFO: Using pid file directory: /var/run/god
I [2018-10-31 23:19:39]  INFO: Started on drbunix:///tmp/god.17165.sock
I [2018-10-31 23:19:39]  INFO: simple move 'unmonitored' to 'init'
I [2018-10-31 23:19:39]  INFO: simple moved 'unmonitored' to 'init'
I [2018-10-31 23:19:39]  INFO: simple [trigger] process is running (ProcessRunning)
I [2018-10-31 23:19:39]  INFO: simple move 'init' to 'up'
I [2018-10-31 23:19:39]  INFO: simple registered 'proc_exit' event for pid 11741
I [2018-10-31 23:19:39]  INFO: simple moved 'init' to 'up'

但是我似乎无法捕获所监视过程的实际输出。永远不会创建或写入“ myprocess.log”文件。

但是除此之外,我还遇到了一些非常奇怪的行为。就像有时在我运行它时,它会喷出无休止的输出流,显示出一个接一个地启动和退出的进程。有时我将文件重命名后会记录到文件中。我无法确定为什么它表现得如此古怪。

上帝0.13.7 /红宝石2.3.0 / OSX 10.13.6

1 个答案:

答案 0 :(得分:1)

检查您再次链接到的文档中的示例:

God.watch do |w|
  w.name = "simple"
  w.start = "ruby /full/path/to/simple.rb"
  w.keepalive
end

您使用的是相对路径,而不是完整路径。如果您尝试使用相对路径,则会出错,并说无法在此处创建日志文件。这将导致它按您所描述的那样在启动/退出之间循环。

此外,请确保在CTRL-C进程god之后,您杀死了后台的ruby进程。您可以看到,即使杀死了god,它也正在与ps aux | grep ruby一起运行。

最后,puts 确实登录到日志文件,但是输出由god缓冲,直到ruby的{​​{1}}进程终止。重复此过程以确认:

simple.rb

切换到新的shell并运行:

# Confirm no running ruby processes, otherwise kill the processes and re-verify
ps aux | grep ruby
# Start the daemon
god -c simple.god -D

我建议您继续阅读ps aux | grep ruby foo 51279 0.0 0.1 4322084 11888 ?? Ss 12:46AM 0:00.09 ruby /Users/foo/simple.rb foo 51241 0.0 0.2 4343944 26208 s000 S+ 12:46AM 0:00.45 ruby /Users/foo/.rvm/gems/ruby-2.6.0-preview2/bin/god -c simple.god -D # Kill the process for simple.rb, which causes god to dump the output to the log and restart it kill 51279 # Verify log file contains expected output cat myprocess.log Hello Hello Hello Hello 的文档。有很多东西,而且答案都在那里。

相关问题