ruby-daemons

How get daemons' status programmatically using Ruby gem "daemons"


I have a script (myscript.rb) like below:

require 'daemons'

Daemons.run_proc 'myproc', dir_mode: :normal, dir: '/path/to/pids' do
  # Daemon code here...
end

So, I can check the daemon's status in console by ruby myscript.rb status.

But I need to show the daemon's status in a web page (Rails), like:

<p>Daemon status: <%= "Daemon status here..." </p>

How can this be done?


Solution

  • In my tests seems like "status" command exits with non-zero value when the daemon is not running:

    $ ruby myscript.rb status; echo $?
    myproc: no instances running
    3
    $ ruby myscript.rb start
    myproc: process with pid 21052 started.
    $ ruby myscript.rb status; echo $?
    myproc: running [pid 21052]
    0
    $ ruby myscript.rb stop
    myproc: trying to stop process with pid 21052...
    myproc: process with pid 21052 successfully stopped.
    $ ruby myscript.rb status; echo $?
    myproc: no instances running
    3
    

    So it is possible to programmatically check the daemon status as below:

    require 'open3'
    
    stdin, stdout, stderr, wait_thr = Open3.popen3('ruby', 'myscript.rb', 'status')
    if wait_thr.value.to_i == 0
      puts "Running"
    else
      puts "Not running"
    end