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

Discussion in 'Python' started by Miki Tebeka, Apr 8, 2011.

  1. Miki Tebeka

    Miki Tebeka Guest

    > 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 <>
    http://pythonwise.blogspot.com
    Miki Tebeka, Apr 8, 2011
    #1
    1. Advertising

  2. Miki Tebeka

    Nobody Guest

    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.
    Nobody, Apr 8, 2011
    #2
    1. Advertising

  3. Re: How to get a PID of a child process from a process openden withPopen()

    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
    Chris Angelico, Apr 8, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. klappnase

    Capture PID of child process

    klappnase, Aug 5, 2003, in forum: Python
    Replies:
    4
    Views:
    475
  2. Tung Wai Yip
    Replies:
    1
    Views:
    757
    Anders Dalvander
    Oct 30, 2003
  3. tazimk

    obtaining pid of child process

    tazimk, Jul 26, 2010, in forum: Python
    Replies:
    1
    Views:
    719
    Chris Rebert
    Jul 26, 2010
  4. P.S.
    Replies:
    0
    Views:
    307
  5. Mav
    Replies:
    0
    Views:
    63
Loading...

Share This Page