how to kill a subprocess

Alain Ketterlin alain at
Fri Sep 10 02:31:25 CEST 2010

cerr <ron.eggler at> writes:

>> x.terminate() (and then x.wait()) where x is the value returned by
>> subprocess.Popen().
> Well, this is what I have:
>   writelog("starting GPS simulator")
>   commandlist=[GPSsim,proto,GPSfile]
>   writelog(commandlist[0]+" "+commandlist[1]+" "+commandlist[2])
>   process=subprocess.Popen(commandlist)
>   writelog("GPS simulator started")
>   ...
>   ...
>   os.kill(,9)
>   os.wait()
> but this is not working for me... :( any clues?

Should be ok. A minor point: os.kill(...) and os.wait() have equivalent
(portable) convenience methods on Popen. Also, it may be better to send
SIGTERM (15) instead of SIGKILL, to ensure the victim can die
gracefully (e.g., remove temp files).

>> P/S: I'm not sure why the python process survives, and I think your use
>> of "zombie" is not correct (afaik a zombie is an exited process whose
>> parent hasn't called wait() yet)
> This is what I have:
> localhost cgi-bin # ps ax | grep py
> 11853 ?        Z      0:00 [python2.6] <defunct>
> 12029 pts/1    S+     0:00 grep --colour=auto py
> The 'Z' you see there stands for Zombie

This is from "man ps":

   Processes marked <defunct> are dead processes (so-called "zombies")
   that remain because their parent has not destroyed them properly. These
   processes will be destroyed by init(8) if the parent process exits.

So the problem is with what launches this python process (11853). If
this process is the python interpreter running the program above, check
how you launch it. If this process is something else, check its parent

-- Alain.

More information about the Python-list mailing list