# Daemons in Rails

**URL:** <https://www.ruby-forum.com/t/daemons-in-rails/113358>\
**Category:** Rails\
**Created:** [August 30, 2007, 10:48am UTC](https://www.ruby-forum.com/t/daemons-in-rails/113358 "2007-08-30T10:48:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![osteo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a87d85/32.png) [@osteo](https://www.ruby-forum.com/u/osteo)\
**Post date:** [August 30, 2007, 10:48am UTC](https://www.ruby-forum.com/t/daemons-in-rails/113358/1 "2007-08-30T10:48:06Z")

</div>

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
