Capture PID of child process

K

klappnase

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
 
M

Michael Hudson

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
 
B

Bernhard Herzog

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
 
B

Bernhard Herzog

Bernhard Herzog said:
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
 
P

P

klappnase said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top