0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
user938363
  • 9,990
  • 38
  • 137
  • 303
  • Important to note when daemonizing: 1) File handles (e.g. for logging) will be closed in the daemon process. So you need to re-create your logger inside `run_proc`. 2) `$stdin`, `$stdout`, `$stderr` will be reopened to point to `/dev/null`. 3) `Process.pid` will have a different value in the daemon. Because of #1, the daemon could actually be running, but you won't see log messages. – Kelvin Feb 28 '19 at 21:47

1 Answers1

2

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:

  • either you have a control script including a Daemons.run_proc call in which you implement the daemon behaviour
  • or you have a control script including a Daemons.run call to which you pass the name of another script that contains the daemon implementation

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.

severin
  • 10,148
  • 1
  • 39
  • 40
  • This helped in that I was using "ruby daemon.rb start" which does not work for me. Based on your answer, I tried "ruby daemon.rb run", this one get me further, but got a permission error in creating 'amine.log' as it tries to crating under top folder "/", how do I configure the options to control both the directory and the name of my logger (i.e. amine.log) – user938363 Nov 19 '13 at 04:43
  • I suggest using an absolute path when instantiating the logger, e.g. `logger = Logger.new(File.join File.dirname(__FILE__), 'amine.log')` to create the logfile in the same directory as the current script... – severin Nov 19 '13 at 10:12
  • An regarding "ruby daemon.rb **run**" vs. "ruby daemon.rb **start**": Using **run** will run the daemon in the foreground (good for debugging), using **start** will properly daemonize the daemon... So as soon as everything works using **run**, you have to use **start** when using the daemon for real... – severin Nov 19 '13 at 10:14
  • Thanks. One more thing, how do I control the directory of the pid file – user938363 Nov 20 '13 at 03:16
  • You can do that using the `:dir` and `:dir_mode` options. See http://daemons.rubyforge.org/Daemons.html#run-method for details! – severin Nov 20 '13 at 09:06