Capture PID of child process

Michael Hudson mwh at python.net
Tue Aug 5 11:51:03 EDT 2003


klappnase at web.de (klappnase) writes:

> Hello, everyone,
> 
> I am running python2.2.2 on a linux box.
> I want to call a shell command and get the PID of this child process
> so I have the possibility to abort the child process while it is still
> running.
> 
> I tried the popen2 module for that:
> 
> self.pp = popen2.Popen3(cmd)
> cmdpid = self.pp.pid
> 
> However I found that the PID returned by self.pp.pid is not the PID of
> the process of interest, but the PID of a subshell in which this child
> process is running.
> So if I do
> 
> os.kill(cmdpid, 9)
> 
> the subshell is killed, but the process I actually wanted to stop is
> happily running on.
> 
> Does anyone have a clue what to do about that? Any help would be very
> appreciated.

If you don't actually want to let a shell near your cmd string, you
can pass a list of strings to popen2.Popen3's constructor, and then
pp.pid will be the PID of the child process you're interested in.

This doesn't seem to be documented anywhere!  But you can read the
source, I hope.

Cheers,
mwh

-- 
58. Fools ignore complexity. Pragmatists suffer it. Some can avoid
    it. Geniuses remove it.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html




More information about the Python-list mailing list