ruby-on-railsrubyubuntudaemonruby-daemons

Why Ruby Daemons.run_proc block was skipped entirely on Ubuntu 12.04


We are trying to to start a daemon with gem daemons on Ubuntu 12.04 and Rails 3.2.12 environment for a ruote worker. Here is the daemon.rb (gem version 1.1.9):

#!/usr/bin/env ruby
require 'daemons'
require 'logger'
  root = Dir.pwd
  Dir.chdir(root)
  file = Dir.pwd + '/ruote_worker.rb' 
  options = {
    :dir_mode   => :normal,
    :dir        => File.join(root, 'amine.log'),
    :log_output => true,
    :backtrace  => true,
    :multiple   => false
  }

  logger = Logger.new('foo.log')
  logger.info('----before daemon----') 

  Daemons.run_proc('ruote_worker', options) do
    # Server loop:
    loop {
      puts '111111111111111'
      logger.info('aaaaaaaaaaaaaaa')
    }
  end

Here is the foo.log:

# Logfile created on 2013-11-10 12:56:12 -0600 by logger.rb/36483
I, [2013-11-10T12:56:12.594196 #26557]  INFO -- : ----before daemon----

Here is the ruote_worker.rb (for testing purpose):

#!/usr/bin/env ruby

require 'logger'
logger = Logger.new('amine.log')

loop do
    logger.info('---amine---')
    puts '*****************************************************  Amine ****************************'
    sleep 5
end

As the log shows, the Daemons.run_proc() block was entirely skipped and not executed at all. We tried Daemons.run_proc() without 'ruote_worker' and Daemons.call() and none of the code inside the block was executed. Somehow the whole block was skipped and never gets executed. How can I make the daemon work? We have limited experience with Ruby daemons gem.


Solution

  • Read through http://daemons.rubyforge.org/ for a good introduction on how to use the daemons gem. Basically, there are two different ways to create a daemon:

    From the looks of what you have already, you were trying to go for the second variant. The following works for me:

    # this is daemon.rb
    require 'daemons'
    Daemons.run('ruote_worker.rb')
    
    
    # this is ruote_worker.rb
    require 'logger'
    logger = Logger.new('amine.log')
    loop do
      logger.info('--- amine ---')
      puts '*** amine ***'
      sleep 5
    end
    

    When you now invoke ruby daemon.rb run (or ruby daemon.rb start) a daemon is started and the script in ruote_worker.rb is executed.