Question

I installed the ruby gem Daemons. To make sure it was working I created a script that would print to a file every 5 seconds. I then created another file to run the script using the trivial example they give you in the readme located at http://daemons.rubyforge.org/ . I require both rubygems and daemons. Then I type 'ruby mycontrol.rb start'. The example they use has some type of message saying '(myserver.rb is now running in the background)', I don't see that, but I don't get any errors. If I do a 'ps -u myusername' I see that the filed to be daemonized is listed in the processes, but doesn't appear to be running as nothing is getting written to the file.

Here is my source:

# this is mycontrol.rb

require 'rubygems'
require 'daemons'

Daemons.run(daemon.rb)

and...

# this is daemon.rb

loop do 
 open('file.out', 'w') do |f|
  f.puts 'hello everybody'
 end
 sleep(3)
end

Does anything I'm doing jump out at you as being wrong?

Thanks, Tony

Was it helpful?

Solution

I've tried your example and it works for me (Ruby 1.8.6 on Linux with Daemons version 1.0.10). However, you may be encountering the following issues:

  • I found that the daemonized process (daemon.rb) was being started with a current working directory of /. This was not the current directory when running mycontrol.rb or the directory that contained daemon.rb. Running as a non-root user meant that my process didn't have permission to write the file. I changed the filename to /tmp/file.out and the file was created with the expected content.

  • You are opening file.out in write-only ('w') mode. This means that it will be truncated and rewritten every 3 seconds. If you open the file in append ('a') mode you will see an additional hello everybody line written to the file every 3 seconds.

I don't see the 'is now running in the background' messages either. I assume this is included in the documentation to illustrate what should have happened rather than to indicate the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top