Python Process that can survive

Christopher Peery peeryc at
Fri Sep 27 15:11:54 EDT 2002

sismex01 at wrote in message news:<mailman.1033137937.24205.python-list at>...
> > From: peeryc at [mailto:peeryc at]
> > 
> > Alright... I love python but I think I may have found a definite
> > problem. I can't seem to run a python process, kick it to the
> > back-ground, and then kill the terminal that started it. With most
> > other things, the python process would keep running in the
> > back-ground regardless. For me, the process always dies with the
> > terminal.
> > 
> > So my question: is there a way to separate a python script from the
> > underlying terminal so that this will not happen.
> > 
> > I'm hoping for some scheme that's supported by the language. I've
> > found a few hacks on-line where the python process is daemonized
> > but this requires shutting down all the standard io streams.
> > There's got to be a better way especially since it can be done in
> > Perl very easily with no special tricks.
> > 
> > By the way, I'm doing this on a linux box.
> > 
> > Chris
> > 
> This same question I asked a few weeks ago, and got a few
> answers back from lotsa nice peepl, so I pass this function
> on to you for it's safekeeping (tag, your it ;-)
> def Daemonize():
>    "Detach the current process from the console."
>    import os, sys
>    if os.fork(): os. exit(0)
>    # Close connection to current console.
>    os.close(sys.  stdin  .fileno())
>    os.close(sys.  stdout  .fileno())
>    os.close(sys.  stderr  .fileno())
>    # Reopen std handles to /dev/null.
>    dev null ="/dev/null", 0)
>    os.dup2(dev null, 0)
>    os.dup2(dev null, 1)
>    os.dup2(dev null, 2)
>    # Create new OS session.
>    os.setsid()
>    # Reset UMASK.
>    os.umask(0)
>    # Refork to have init as parent.
>    if os.fork(): os. exit(0)
> This is a synthesis of several replies I received, and it
> seems to work, but others might have some observations
> to make to this little function.
> HTH!
> -gustavo
> Advertencia: 
> La informacion contenida en este mensaje es confidencial y restringida 
> y
> esta destinada unicamente para el uso de la persona arriba indicada, 
> Esta
> comunicacion representa la opinion personal del remitente y no refleja
> necesariamente la opinion de la Compa ia. Se le notifica que esta
> estrictamente prohibida cualquier difusion, distribucion o copia de 
> este
> mensaje. Si ha recibido esta comunicacion o copia de este mensaje por 
> error,
> o si hay problemas en la transmision, favor de comunicarse con el 
> remitente.
> Todo el correo electr nico enviado para o desde esta direcci n 
> ser 
> procesado por el sistema de correo corporativo de HEB. Tal correo
> electr nico esta sujeto a ser almacenado y puede ser revisado por 
> alguien
> ajeno al recipiente autorizado con el prop sito de monitorear que se 
> cumplan
> las normas de seguridad de la empresa.

Yeah I found this hack on the web and I can't believe that this is
only way to do this in python. You're basically stripping the process
of all it's stream and sending it on its way. This is really messy.

Plus in my lab there are a bunch of perl guys and a few python people.
To date, what one can do in their script the python guys can do to
(only nicer and much more readible). Perl has the ability to have a
process continue after the underlying terminal is killed if the
process is in the background. What is python doing that doesn't allow


More information about the Python-list mailing list