[UNIX] Detaching from parent process

Michael P. Reilly arcege at shore.net
Mon May 8 15:01:49 EDT 2000

Florian Weimer <Florian.Weimer at RUS.Uni-Stuttgart.DE> wrote:
: I'm trying to implement a CVS loginfo script with Python.  The script
: has to fork to the background, because the parent process has to
: release the repository lock.  But it seems as if Python doesn't detach
: completely from the parent process, because CVS exists only after the
: lengthy operation has completed.

: Currently I use the following code:

:     if os.fork() == 0:
:         os.setsid()
:         if os.fork() == 0:
:             os.close(0)
:             os.close(1)
:             os.close(2)
:             os.open("/dev/null", os.O_RDWR)
:             os.dup(0)
:             os.dup(0)
:             # Lengthy operation claiming the repository lock follows.

It is likely that you've either found your solution or that someone
(probably Donn Cave :) has piped up in an email to you.  But you
probably want to close the file descriptors (and files) before you call
setsid(), in the child process, not the grandchild.  If stdin/stdout/
stderr are still opened to the controlling terminal, then setsid()
could still have a problem disassociating itself from the controlling

It might not be that, but it sounds likely on some UNIX systems.

More information about the Python-list mailing list