FAQ
Hello,

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

Search Discussions

  • Miki Tebeka at Apr 8, 2011 at 2:43 pm

    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.

    HTH
    --
    Miki Tebeka <miki.tebeka at gmail.com>
    http://pythonwise.blogspot.com
  • Nobody at Apr 8, 2011 at 7:28 pm

    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 <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.
  • Chris Angelico at Apr 8, 2011 at 10:35 pm

    On Sat, Apr 9, 2011 at 5:28 AM, Nobody wrote:
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 8, '11 at 10:40a
activeApr 8, '11 at 10:35p
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase