From: fuzzy at fuzzys.org (Julien Oster)
Date: 12 Apr 1999 14:29:28 +0200
Subject: forking + stdout = confusion
References: <RUgQ2.31$Oq4.32657@newsfeed.avtel.net>
Message-ID: <7jlnfyowd3.fsf@gandalf.midearth.fuzzys.org>
Content-Length: 2067
X-UID: 1180                                                  

>>>>> "Clarence" == Clarence Gardner <clarence at silcom.com> writes:

    Clarence> I have a program running in Python 1.5.1 under FreeBSD 2.1.7.
    Clarence> It happens to be a nph-type CGI program.  Normally, it runs a
    Clarence> report that can take quite a while, and I'm trying to add an
    Clarence> option to delay the start.  I defined this function:

[...]

I ran in exactly the same problem. I tried closing all stdxxx-filedescriptors, 
setting a new session id with setsid, also tried it with setting a new
progress groups, and I even tried to reopen stdin, stderr and stdout to
/dev/null, but nothing helped. Then I saw that in OpenBSD, there's a function
in stdlib.h called "daemon()". You simply call it, and it puts the running
program in the background (it forks and exits the parent), redirects the
stdxxx-descriptors, sets a new session ID etc... and with this function, it
works!

Anyway, I was not able to reproduce what daemon does in any way, obviously it
does anything else which I did not think of, but I can't find out what. So for
now I use a very dirty wrapper which just calls the daemon-function, but since
I also realized that at least linux libc5 (I don't know how this is with
glibc) doesn't know this handy function, it would be fine to know what I
forgot while trying to reproduce daemon()'s actions.

I think it must be something with the stdxxx-filedescriptors and/or the
terminal, since using daemon() in a cgi lets apache close the connection to
the browser, but the simple call of fork, stdxxx.close/reopen/whatever, setsid 
and setpgrp does not.

For know, check if you're system is having the daemon() function (it should
reside in the C-Library and its declaration in stdlib.h) and if it has, use it 
either with a small C wrapper program, with a python module which implements
it or even using the dl-module.

-- 
  /--/ Julien Oster /---/ www.fuzzys.org <---> www.sysadm.cc /---/
 /--/ OpenBSD  2.5 /---/ Greetings  from  Munich,   Germany /---/
/--/ contact me : /---/ talk fuzzy at fuzzys.org or e-Mail me /---/




