Capture PID of child process

Bernhard Herzog bh at intevation.de
Tue Aug 5 18:01:13 CEST 2003


klappnase at web.de (klappnase) writes:

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

If cmd is a string popen will start a sub-shell so that'd the pid you
get. You could try to prefix your command with exec so that your
command's process replaces the shell process (see the shell
documentation). 

Alternatively you could use a list of strings as the command so that no
shell is used in the first place, i.e. "prog param" would become
["prog", "param"]. This approach has the advantage that you don't have
to worry about shell quoting but it's less portable because it's not
even documented (I'll file a bug report for that).

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
Thuban                                  http://thuban.intevation.org/




More information about the Python-list mailing list