[2.4.2/Linux] Getting Python to fork?

Christian Heimes lists at cheimes.de
Mon Feb 4 09:40:49 EST 2008


Jon Ribbens wrote:
> This should work I believe:
> 
>   if os.fork():
>     os._exit(0)
>   os.setsid()
>   os.chdir("/")
>   fd = os.open("/dev/null", os.O_RDWR)
>   os.dup2(fd, 0)
>   os.dup2(fd, 1)
>   os.dup2(fd, 2)
>   if fd > 2:
>     os.close(fd)
>   # do stuff
> 
> Although bear in mind it's pretty UNIX-y.

IIRC you have to fork a second time after you have changed the working
dir and created a new session group.

Christian




More information about the Python-list mailing list