473,473 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A way to discover PIDs of child processes?

Hi all!

I'm launching a subprocess using the following code, and I'd like to
kill off all associated subprocesses after a given timeout:

myproc = popen2.Popen3(command)

Now, because the command is executed in the shell, I end up with the
following process tree:

PID PPID PGID WINPID TTY UID STIME COMMAND
2332 3156 2332 3412 con 1012 15:34:11
/usr/bin/python2.4
3068 2332 2332 2268 con 1012 15:34:11 /usr/bin/sh
1584 3068 2332 2620 con 1012 15:34:12
/cygdrive/c/GNATPRO/5.01
a/bin/powerpc-elf-gdb
Here are the two options I've come up with:
1) I can kill 3068 using os.kill(myproc.pid, signal.SIGKILL), but that
keeps 1584 running and therefore doesn't do the trick.

2) I can kill the group of processes (2332) using
os.killpg(os.getpgid(myproc.id), signal.SIGKILL), but that terminates
Python as well, and I'd rather continue on my merry way in Python...

Here's my code:

myproc = popen2.Popen3(command)

second_count = 0
while myproc.poll() == -1:
time.sleep(1) # wait a sec
second_count = second_count + 1
if second_count == 30:
##use commented code to kill group
##pgid = os.getpgid(myproc.pid)
##os.killpg(pgid, signal.SIGKILL)
os.kill(myproc.pid, signal.SIGKILL)

One solution I've considered is using the Popen class to launch the
subprocess with shell=False. This would eliminate the shell process so
I'd only have one process to kill.... but I've got some I/O redirection
in my command string that I'd like to pass along to the shell. I think
I need the shell there for other reasons as well. Any idea how I can
know if a given process has children and find out what their PIDs are?

Any suggestions are greatly appreciated!

Feb 17 '06 #1
2 11182
In article <11**********************@g44g2000cwa.googlegroups .com>,
sc*********@yahoo.com wrote:
Now, because the command is executed in the shell, I end up with the
following process tree:

PID PPID PGID WINPID TTY UID STIME COMMAND
2332 3156 2332 3412 con 1012 15:34:11
/usr/bin/python2.4
3068 2332 2332 2268 con 1012 15:34:11 /usr/bin/sh
1584 3068 2332 2620 con 1012 15:34:12
/cygdrive/c/GNATPRO/5.01
a/bin/powerpc-elf-gdb


It doesn't always have to be that way, depending on the
nature of the command. If it's the last thing the shell
has to do, then it can be exec'ed without a fork, which
leaves the gdb image running in the immediate child process.

Some shells do that automatically. In any case, a Bourne
shell "exec" statement will do it, like "exec /.../gdb",
with whatever redirections etc.

Donn Cave, do**@u.washington.edu
Feb 17 '06 #2
Donn,

Your suggestion worked perfectly. Thank you for showing me the forest
from the trees :-)

Feb 17 '06 #3

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

Similar topics

1
by: Markus Franz | last post by:
Hi. I created a little script: for currenturl in sys.argv: pid = os.fork() if pid == 0: signal.alarm(10) do_something() # placeholder for the download and print routine
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: Rech | last post by:
Hi, I need a little help here managing child processes in Python. I'm not so skilled in system programming so I hope you can give me some good suggestions. I have a very CPU and memory...
1
by: Jason Godden | last post by:
Hi All, I've written a few C functions in the version 1 format as well as some SPI based backend bits and I'm curious as to how PG treats these items when used within plpgsql. The set of...
4
by: Jeremy Wallace | last post by:
For the purposes of reconstructing an MDW, should the worst ever come about, is there any incentive for me to record user account PIDs? Certainly there's plenty of incentive to record PIDs for all...
0
by: Wizou | last post by:
1. Create a TcpListener 2. Start a child Process 3. Kill the parent process => You can't bind to the port until you close the child process Variant : 3. Terminate normally the parent process...
2
by: Fred Heida | last post by:
Hi, Question on Vista Run As Administrator. If i have exe which creates a child process, using the Process class, and this exe is Run As Asministror, is there a way to have the child process...
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...
2
by: rocco.rossi | last post by:
I'm using the Python processing module. I've just run into a problem though. Actually, it's a more general problem that isn't specific to this module, but to the handling of Unix (Linux processes)...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.