I would like to have one of my ruby programs running in the background at all
times like a normal UNIX daemon (it is a daemon logging temperature data to a
database). I thought this would be a easy enough task since all the syscalls
is right there from the start. I somehow don't manage to get it right anyway..
I tried do write something along the lines of the BSD call daemon(3):

--------------------------------------------------------------------------
def daemon(doClose, doChdir)

        if fork != nil
                exit(0);
        end

        Process.setsid

        if doChdir 
                Dir.chdir("/")
        end

        if doClose
                $stdin.close
                $stdout.close
                $stderr.close
                $stdin = $stdout = $stderr = open("/dev/null", "rw")
        end

end

[ read some args etc ]

if fgFlag
        main
else
        print "Fork to background! \n"
        daemon(true, true)
        main
end
--------------------------------------------------------------------------

and main is the mainloop..

this doesn't play at all - main isn't even called.. if I change the daemon
method call to daemon(false, true) however I seem to fork into the background
and execute main as expected. I then get output to the screen if I don't add
some "if fgFlag ..." statements where output is generated. That is kind of
fine - I should do that anyway, but I can't really understand what could
possibly cause everything to quit when I execute the code inside the doClose
clause.

Anyone able to enlighten me?

-- 
-----------------------------------------------------------.
Patrik Sundberg  email: ps / raditex.se || ps / radiac.mine.nu |
                 Phone: +46 13 178567 || +46 707 602240    |
.---> UNIX consultant                                      |
|-----> Applied Physics and Electrical Engineering student |
`----------------------------------------------------------'