Capture PID of child process

Discussion in 'Python' started by klappnase, Aug 5, 2003.

  1. klappnase

    klappnase Guest

    Hello, everyone,

    I am running python2.2.2 on a linux box.
    I want to call a shell command and get the PID of this child process
    so I have the possibility to abort the child process while it is still
    running.

    I tried the popen2 module for that:

    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.
    So if I do

    os.kill(cmdpid, 9)

    the subshell is killed, but the process I actually wanted to stop is
    happily running on.

    Does anyone have a clue what to do about that? Any help would be very
    appreciated.

    Thank you in advance

    Michael
     
    klappnase, Aug 5, 2003
    #1
    1. Advertising

  2. (klappnase) writes:

    > Hello, everyone,
    >
    > I am running python2.2.2 on a linux box.
    > I want to call a shell command and get the PID of this child process
    > so I have the possibility to abort the child process while it is still
    > running.
    >
    > I tried the popen2 module for that:
    >
    > 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.
    > So if I do
    >
    > os.kill(cmdpid, 9)
    >
    > the subshell is killed, but the process I actually wanted to stop is
    > happily running on.
    >
    > Does anyone have a clue what to do about that? Any help would be very
    > appreciated.


    If you don't actually want to let a shell near your cmd string, you
    can pass a list of strings to popen2.Popen3's constructor, and then
    pp.pid will be the PID of the child process you're interested in.

    This doesn't seem to be documented anywhere! But you can read the
    source, I hope.

    Cheers,
    mwh

    --
    58. Fools ignore complexity. Pragmatists suffer it. Some can avoid
    it. Geniuses remove it.
    -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
     
    Michael Hudson, Aug 5, 2003
    #2
    1. Advertising

  3. (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/
     
    Bernhard Herzog, Aug 5, 2003
    #3
  4. Bernhard Herzog <> writes:

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


    As it turns out, there already is a bug report:
    http://python.org/sf/666700

    Bernhard

    --
    Intevation GmbH http://intevation.de/
    Sketch http://sketch.sourceforge.net/
    Thuban http://thuban.intevation.org/
     
    Bernhard Herzog, Aug 5, 2003
    #4
  5. klappnase

    Guest

    klappnase wrote:
    > Hello, everyone,
    >
    > I am running python2.2.2 on a linux box.
    > I want to call a shell command and get the PID of this child process
    > so I have the possibility to abort the child process while it is still
    > running.
    >
    > I tried the popen2 module for that:
    >
    > self.pp = popen2.Popen3(cmd)


    I modified this a bit to create my own class,
    which allows me to kill the whole process group
    and wait for completion. See the fslint_backend
    class in http://www.pixelbeat.org/fslint/FSlint-2.02.tar.gz

    Pádraig.
     
    , Aug 6, 2003
    #5
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,212
    David Schwartz
    Dec 9, 2003
  2. tazimk

    obtaining pid of child process

    tazimk, Jul 26, 2010, in forum: Python
    Replies:
    1
    Views:
    765
    Chris Rebert
    Jul 26, 2010
  3. P.S.
    Replies:
    0
    Views:
    357
  4. Miki Tebeka
    Replies:
    2
    Views:
    637
    Chris Angelico
    Apr 8, 2011
  5. Z W
    Replies:
    0
    Views:
    197
Loading...

Share This Page