rubygod

How to capture process output using God?


Trying to get a simple God demo working.

In an empty directory I created the following files as per the God documentation:

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

Then I run:
$ sudo god -c simple.god -D

and get this output:

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'

but I can't seem to capture the actual output from the watched process. The 'myprocess.log' file never gets created or written to.

But beyond that I'm just experiencing some really weird behavior. Like sometimes when I run it it spews an endless stream of output showing processes starting and exiting one after another. Sometimes it logs to files after I've renamed them. I can't get a peg on why it's behaving so erratically.

God 0.13.7 / ruby 2.3.0 / OSX 10.13.6


Solution

  • Check the example in the documentation that you linked to again:

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

    You are using a relative path, not a full path. If you try to use a relative path it's going to error out and say it can't create the log file there. This will cause it to loop through start/exit as you described.

    Also, make sure that after you CTRL-C the god process that you kill your backgrounded ruby process. You can see that even after killing god that it's running with ps aux | grep ruby.

    Finally, puts does log to the log file, but the output is buffered by god until the ruby process for simple.rb is terminated. Repeat this process to confirm:

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

    Switch to a new shell and run:

    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
    

    I recommend you keep reading the documentation for god. There's a lot to it, and the answers are all there.