subprocess leaves child living

Rob Wolfe rw at smsnet.pl
Tue Jun 5 10:06:15 EDT 2007


Thomas Dybdahl Ahle wrote:

> Problem is - I can't do that when I get killed.
> Isn't it possible to open processes in such a way like terminals? If I
> kill the terminal, everything open in it will die too.

On POSIX platform you can use signals and ``os.kill`` function.
Fo example:

<code>
import os, signal
from subprocess import Popen
from time import sleep

def handler(signum, frame):
    print 'Signal handler called'
    raise KeyboardInterrupt

signal.signal(signal.SIGTERM, handler)

try:
    popen = Popen(["ping", "google.com"])
    try:
        sleep(100)
    except KeyboardInterrupt:
        pass
finally:
    if popen.poll() is None:
        print "killing process: %d" % popen.pid
        os.kill(popen.pid, signal.SIGTERM)
</code>

--
HTH,
Rob




More information about the Python-list mailing list