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

413 views Asked by At

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?

2

There are 2 answers

0
Eduardo Henrique Bogoni On BEST ANSWER

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
0
thomasius On

The defaut way to do this would really be to call ruby myscript.rb status from your Rails application and parse its output to obtain the desired information. Alternatively you could also create an Application object itself and call #running? on it.