I am trying to run the same script in multiple daemons.
myapp.rb looks like this:
loop do
sleep 5
1 / 0 # crash it
end
my myapp_controller.rb:
require 'rubygems'
require 'daemons'
options = {
:log_output => true,
:backtrace => true,
:monitor => true,
:multiple => true,
:log_dir => '/mnt/log/',
:hard_exit => true
}
Daemons.run(File.join(File.dirname(__FILE__), 'myapp.rb'), options)
When I run ruby myapp_controller.rb start several times in a row, it creates that many daemons, as I expect. But, after a while, due to an error in myapp.rb the daemons crash and the monitor restarts just one and not all. So I end up with a single running daemon.
Why? What am I doing wrong?
I was able to reproduce the behavior. It is not anything you are doing wrong; it is the way the
daemons gembehaves.Going through the code for the daemons gem, turns out the
:multipleoption doesn’t work well with the:monitoroption.The
:monitoroption works only when the daemon is run in single mode.I have created a bug report on the daemons project page referencing this question as the source.
More info about the reproduction of the issue:
Multiple daemon processes are created when
:multiple => true. Each process has its own pid file in the format of<scriptname>.rb<number>.pid.However, only one monitor process is created (with a single
<scriptname>.rb_monitor.pidfile.)Here are the list of processes started when I start the daemon process 3 times:
The files in the pid/log folder: