How to get a PID of a child process from a process openden with Popen()

Nobody nobody at
Fri Apr 8 15:28:43 EDT 2011

On Fri, 08 Apr 2011 07:43:41 -0700, Miki Tebeka wrote:

>> 	p = subprocess.Popen(shlex.split("kdesu -u test program"))
>> How can I aquire the PID of the program which kdesu starts?
> You can run "ps --ppid <>" and get the line containing test program.
> The first field there should be the child process id.

This will fail if the kdesu process has terminated at that point (the
documentation doesn't say whether it waits for the child to terminate).
Once a process' parent has terminated, it's PPID will become 1 (i.e. it
will be "adopted" by the init process).

There isn't a robust solution to the OP's problem. It's typically
impossible to determine whether one process is an ancestor of another if
any of the intermediate processes have terminated.

More information about the Python-list mailing list