代码之家  ›  专栏  ›  技术社区  ›  Yarin

如何使用God捕获进程输出?

  •  1
  • Yarin  · 技术社区  · 7 年前

    God 演示正在运行。

    简单.rb:

    loop do
      puts 'Hello'
      sleep 1
    end
    

    简单.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 回复  |  直到 7 年前
        1
  •  1
  •   anothermh ScarletMerlin    7 年前

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

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

    CTRL-C 这个 god 你杀死你的背景的过程 ruby 过程。即使杀人后你也能看到 上帝啊 ps aux | grep ruby

    最后, puts 上帝啊 直到 红宝石 过程 simple.rb

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

    切换到新shell并运行:

    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
    

    上帝啊 . 有很多,答案都在那里。

    推荐文章