Daemons in Rails

I tried for quite a few hours in vain to use the Daemons module for
Rails. Ultimately, Rails was always trying to write to STDOUT and that
is not allowed for real daemons. No matter where I reopened STDOUT to
write to a file, Rails always seemed to have a reference to the closed
one.

Anyway, so I ended up writing my own daemon process. It works, but
still remains connected to the shell, so that when I exit, I need to
kill my ssh session. The daemon still runs, but it’s annoying.

Here is my code. I tried to emulate what Daemons is supposed to do, but
of course I don’t do everything (e.g. I don’t set the umask, etc.).
Does anyone have any idea why this would remain attached to the tty?

Regardless, I think this code is probably helpful to people.

Thanks.

if (@@DAEMON)
puts "Running as daemon. Output in " + ENV[“PWD”] + “/log”
$stdout = STDOUT.reopen(“log/import-stdout.log”, “w”)
$stderr = STDERR.reopen(“log/import-stderr.err”, “w”)
pid = fork
if pid
Process.detach(pid)
exit
else
Process.setsid
pid = fork
if pid
Process.detach(pid)
exit
end
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs