473,786 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c md)
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 8765
kl*******@web.d e (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(c md)
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.d e (klappnase) writes:
self.pp = popen2.Popen3(c md)
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(c md)


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
7052
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 os.kill(self.popen.pid, signal.SIGTERM) Everything works fine on MacOSX. However, I'm doing a port to Solaris (so I can run it on my web site) and find that the child processes are not stopping! Solaris is creating TWO new processes: one for the SHELL...
2
2926
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 this knowledge base article. ( http://support.microsoft.com/?id=319985 ) to execute a DTS package and monitor it's events. I'm not able to capture the events from a child package that is called from an Execute Package task. I've seen a few...
3
6957
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 ChildForm(); this.AddOwnedForm (child);
2
278
by: Joe | last post by:
Is it possible to capture the File I/O of a child process? If so, how? Thanks!
10
4029
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
0
1363
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 in my Application entry point: Public Shared Sub Main() 'Explicitly set apartment state to Single Thread Apartment (STA) System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA Dim eh As New MyExceptionHandler()
4
3370
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 child form but I can only capture the visible part of the child form whatever you scroll to any part of the form. I have read the article in site http://www.fengyuan.com/article/wmprint.html unforturely it is for C++ instead of VB.Net. I tried to...
2
2852
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. But running the script as a child process only sets values for that process, which are lost after execution. So I thought I could simply tack on an 'env' command line to the script input lines as shown below. However, using subprocess.Popen gives...
22
11756
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 in my situation. I first created a few child processes with Popen, then in a while(True) loop wait on any of the child process to exit, then restart a child process: import os from subprocess import Popen
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.