title: Ruby Daemon gem how to avoid exit errors?
body: <p>I am running the following script with the <code>$ruby script_name</code> command. The parent script spawns two child processes and waits for a SIGTERM or a SIGINT. The parent script is able to terminate the child processes as expected. However, on inspecting the child log I notice an error has been logged during exit. What causes such an error and how can I avoid them to exit cleanly?</p>
  
  <p>Parent/Child Script <code>worker_monitor.rb</code>:</p>
  
  <pre><code>require 'rubygems'
  require 'daemons'
  require 'fileutils'
  
  class SignalHandler
    attr_accessor :term
  
    def initialize(signal_str, name)
      @term = false
      Signal.trap(signal_str) do
        puts "#{name} #{signal_str} trapped"
        @term = true
      end
    end
  end
  
  cwd = Dir.pwd
  
  options = {
    :multiple =&gt; true,
    :dir_mode =&gt; :normal,
    :dir =&gt; cwd,
    :backtrace =&gt; true,
    :log_output =&gt; true,
  }
  
  options1 = options.merge({ :log_dir =&gt; "#{cwd}/tmp/daemon/task1", :app_name =&gt; 'worker' })
  FileUtils.mkdir_p("#{cwd}/tmp/daemon/task1")
  task1 = Daemons.call(options1) do
  
    handler = SignalHandler.new("TERM", 'Task1')
  
    puts "Entered task1 (pid = #{Process.pid}, ppid = #{Process.ppid}), looping..."
    while !handler.term do
      puts "Task1 looped"
      sleep(5)
    end
  
    puts "Task1 stopped."
  end
  
  sleep(1) until task1.pid.running?()
  
  puts "Check task1 status"
  task1.show_status()
  
  
  options2 = options.merge({ :log_dir =&gt; "#{cwd}/tmp/daemon/task2", :app_name =&gt; 'iamnotworker' })
  FileUtils.mkdir_p("#{cwd}/tmp/daemon/task2")
  task2 = Daemons.call(options2) do
  
    handler = SignalHandler.new("TERM", 'Task2')
  
    puts "Entered task2 (pid = #{Process.pid}, ppid = #{Process.ppid}), looping..."
    while !handler.term do
      puts "Task2 looped"
      sleep(5)
    end
  
    puts "Task2 stopped."
  end
  
  sleep(1) until task2.pid.running?()
  
  puts "Check task2 status"
  task2.show_status()
  
  
  puts "Monitor (pid = #{Process.pid}) created children, looping ..."
  
  
  handler_term = SignalHandler.new("TERM", 'Monitor')
  handler_int = SignalHandler.new("INT", 'Monitor')
  
  
  while !handler_term.term &amp;&amp; !handler_int.term do
    sleep(5)
  end
  
  puts "Monitor exit loop."
  
  puts "Stopping task1"
  task1.stop()
  puts "Stopping task2"
  task2.stop()
  
  sleep(1) while task1.pid.running?
  sleep(1) while task2.pid.running?
  
  sleep(2)
  </code></pre>
  
  <p>Script StdOut:</p>
  
  <pre><code>Check task1 status
  worker: running [pid 4977]
  Check task2 status
  worker: running [pid 4983]
  Monitor (pid = 4972) created children, looping ...
  Monitor TERM trapped
  Monitor exit loop.
  Stopping task1
  worker: trying to stop process with pid 4977...
  worker: process with pid 4977 successfully stopped.
  Stopping task2
  worker: trying to stop process with pid 4983...
  worker: process with pid 4983 successfully stopped.
  </code></pre>
  
  <p>Task1 Output log (Task2 has a similar log):</p>
  
  <pre><code># Logfile created on 2013-06-24 22:26:55 -0700 by logger.rb/31641
  I, [2013-06-24T22:26:55.281375 #4977]  INFO -- : *** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
  E, [2013-06-24T22:26:55.281533 #4977] ERROR -- : exit (SystemExit)
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/daemonize.rb:84:in `exit'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/daemonize.rb:84:in `call_as_daemon'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/application.rb:259:in `start_proc'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/application.rb:296:in `start'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons.rb:252:in `call'
  lib/worker_monitor.rb:29:in `&lt;main&gt;'
  I, [2013-06-24T22:26:55.281754 #4977]  INFO -- : *** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
  E, [2013-06-24T22:26:55.283349 #4977] ERROR -- : stream closed (IOError)
  
  E, [2013-06-24T22:26:55.283506 #4977] ERROR -- : failed to allocate memory (NoMemoryError)
  
  E, [2013-06-24T22:26:55.283728 #4977] ERROR -- : stack level too deep (SystemStackError)
  
  E, [2013-06-24T22:26:55.284156 #4977] ERROR -- : exception reentered (fatal)
  
  E, [2013-06-24T22:26:55.284424 #4977] ERROR -- : exit (SystemExit)
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/daemonize.rb:84:in `exit'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/daemonize.rb:84:in `call_as_daemon'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/application.rb:259:in `start_proc'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons/application.rb:296:in `start'
  /home/user/.rvm/gems/ruby-1.9.3-p429/gems/daemons-1.1.9/lib/daemons.rb:252:in `call'
  lib/worker_monitor.rb:29:in `&lt;main&gt;'
  </code></pre>