I am starting a GUI-application as another user with kdesu in my python
script:
import shlex, subprocess
p = subprocess.Popen(shlex.split("kdesu -u test program"))
How can I aquire the PID of the program which kdesu starts?
p.pid just returns the PID of kdesu, but I need the PID of the
child process from kdesu.
My System: openSUSE 11.4 64-Bit, Python 2.7.
Regards
Pedro Santos
HTH
--
Miki Tebeka <miki....@gmail.com>
http://pythonwise.blogspot.com
>> 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 <p.pid>" 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.
Upstart and gdb can both detect forks and follow the child. But I
think that's getting into some serious esoteria that's unlikely to be
of much practical use here.
Chris Angelico