Python Process that can survive

sismex01 at sismex01 at
Fri Sep 27 10:41:33 EDT 2002

> 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.

   # 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.

   # Reset UMASK.

   # 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.



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.

More information about the Python-list mailing list