473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Capture PID of child process

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
Jul 18 '05 #1
4 8745
kl*******@web.de (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
Jul 18 '05 #2
kl*******@web.de (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/
Jul 18 '05 #3
Bernhard Herzog <bh@intevation.de> 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/
Jul 18 '05 #4
P
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.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
2
by: Rich Tasker | last post by:
My goal is to execute a DTS package that calls multiple child DTS packages from a C# (2003) app and display the progress of the entire process to the user. I have followed the model defined in...
3
by: Frank T. Clark | last post by:
How do I redirect or capture keydown events in a parent form from a child form? I have a main form which displays another informational form marked "SizableToolWindow". Form child = new...
2
by: Joe | last post by:
Is it possible to capture the File I/O of a child process? If so, how? Thanks!
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
0
by: JWA | last post by:
Hi All, How can you capture unhandled exceptions thrown by an MDI child form separate from the entire app? I have an application-wide exception handler to catch crashes by doing the following...
4
by: jxiang | last post by:
I created a child form that is much bigger than the MDI form in VB.Net. I am trying to capture the whole child form and save as an image or sent to printer. I tried to use BitBlt to capture the...
2
by: Gerard Flanagan | last post by:
Hello, I have a third party shell script which updates multiple environment values, and I want to investigate (and ultimately capture to python) the environment state after the script has run....
22
by: Jason Zheng | last post by:
This may be a silly question but is possible for os.wait() to lose track of child processes? I'm running Python 2.4.4 on Linux kernel 2.6.20 (i686), gcc4.1.1, and glibc-2.5. Here's what happened...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.