I'm in the same boat you are at the moment, learning the various ways of
daemonizing code. So far, I've gotten a couple methods working.

First, have you tried forking your code? Look at the method described here:
http://soa.dzone.com/snippets/daemonize-ruby-process

That was the first thing that I got working successfully. Of course you
have to manually kill the process afterward, using the 'kill' command in
the terminal, because there isn't an automated way. To make my life easier,
between the "pid = fork do" and the "loop do" I added this line: "puts
"\'script_name\' pid is set to #{Process.pid}"" so I can make a note of
what the pid is when I need to kill it later.

The second thing I got working was Daemons. Daemons' documentation is
easier to read than the documentation for some other daemon gems. With
Daemons, you have your code, let's suppose it is in 'file_checker.rb'.
Create a new file, 'file_checker_daemon.rb'. Then in
'file_checker_daemon.rb' put the following code, as explained in the
Daemons documentation at http://rubydoc.info/gems/daemons/1.1.9/frames:

require 'daemons'
Daemons.run_proc do
   loop do
      sleep(5)
   end
end

Afterwards, to daemonize your code simply do

ruby file_checker_daemon.rb start

and to stop it do

ruby file_checker_daemon.rb stop

Daemons will take care of remembering the pid, and the 'start' and 'stop'
methods make it super easy to control, unlike the manual process of forking
your code.

Hope that helps. Right now I'm trying to figure out how to do the same
thing using Dante and God. Maybe someone can help me? Dante seems to be for
webservers only, I can't seem to find in the documentation how to simply
daemonize some code. And for God, I have the following config, but it's not
working, I keep getting "start command exited with non-zero code = 1".

filename is 'script.god'

God.watch do |w|
  w.name = "script"
  w.dir = "/Users/me/ruby/"
  w.pid_file = w.pid_file = File.join("log/script.pid")
  w.log = "log/script.log"
  w.start = "script.rb -P /log/script.pid  -d"
end

Any suggestions, what am I doing wrong??

Thanks!




On Thu, Feb 20, 2014 at 8:45 AM, cynic limbu <lists / ruby-forum.com> wrote:

> I'm having a problem using daemons (it's a gem used for running ruby
> scripts in the background)
>
> There are number of tutorials floating on the web but I seriously cannot
> understand.
>
> Can someone shorten it and explain it to me? Thank you.
>
> I'd like to run the following code in the background
>
>
> Dir.chdir ENV['HOME']
> if File.exists?("importantfile.rb")
> nil
> else
> newfile = File.new("importantfile.rb", "w")
> newfile.print("something")
> newfile.close
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>