473,758 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proper way to kill child processes

My application starts up a number of processes for various purposes using:
self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.p y")
and then shuts them down when appropriate with
os.kill(self.po pen.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 and then
another started by the shell to run my Python script. The os.kill() is
killing the shell process, not the one running my Python code.

Actually, I really want to kill both of these processes, but I only have the
pid for the shell process. I cannot kill the whole process group because
that kills the main process, too (I tried it).

So, what is the best way to kill both the shell process (whose pid is
available from the Popen3 object) and its child process that is running my
Python script? It looks like the python script process id is always one
greater than the shell process id of the shell process, but I'm sure I
cannot rely on that.

Thanks,

Bob Swerdlow
COO
Transpose
rs*******@trans pose.com
207-781-8284
http://www.transpose.com

----------------------------------
Fight Spam!
Add this link to your signature (as I did): http://wecanstopspam.org
Click through to find out more.
----------------------------------

Jul 18 '05 #1
6 7051

Try a different format for the cmd:

self.popen = popen2.Popen3(["/usr/local/bin/python", "-O",
"myscript.p y"])

i.e. pass the cmd as a list of the individual arguments. That avoids
invoking the sh. See <your_python_in stall>/Lib/popen2.py for more
details.

/Jean Brouwers
ProphICy Semiconductor, Inc.

In article <ma************ *************** **********@pyth on.org>, Bob
Swerdlow <rs*******@tran spose.com> wrote:
My application starts up a number of processes for various purposes using:
self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.p y")
and then shuts them down when appropriate with
os.kill(self.po pen.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 and then
another started by the shell to run my Python script. The os.kill() is
killing the shell process, not the one running my Python code.

Actually, I really want to kill both of these processes, but I only have the
pid for the shell process. I cannot kill the whole process group because
that kills the main process, too (I tried it).

So, what is the best way to kill both the shell process (whose pid is
available from the Popen3 object) and its child process that is running my
Python script? It looks like the python script process id is always one
greater than the shell process id of the shell process, but I'm sure I
cannot rely on that.

Thanks,

Bob Swerdlow
COO
Transpose
rs*******@trans pose.com
207-781-8284
http://www.transpose.com

----------------------------------
Fight Spam!
Add this link to your signature (as I did): http://wecanstopspam.org
Click through to find out more.
----------------------------------

Jul 18 '05 #2
Bob Swerdlow wrote:
My application starts up a number of processes for various purposes
using:
self.popen = popen2.Popen3("/usr/local/bin/python -O
"myscript.p y")


This works for me:

self.popen = popen2.Popen3(["python", "/usr/local/bin/python",
"-O", "myscript.p y"])

But I don't know if its a hack or a desired feature.

Mathias
Jul 18 '05 #3

It should be the other way around

self.popen = popen2.Popen3(["/usr/local/bin/python",
"-O", "myscript.p y"])

Item [0] must be the path to the executable and items[1:] are the
arguments. See the Popen3._run_cmd () method in Lib/popen2.py
partially copied here:

<pre>
def _run_child(self , cmd):
if isinstance(cmd, basestring):
cmd = ['/bin/sh', '-c', cmd]
.....
try:
os.execvp(cmd[0], cmd)
finally:
os._exit(1)
</pre>

/Jean Brouwers
ProphICy Semiconductor, Inc.

In article <17************ @valpo.de>, Mathias Waack <M.*****@gmx.de >
wrote:
Bob Swerdlow wrote:
My application starts up a number of processes for various purposes
using:
self.popen = popen2.Popen3("/usr/local/bin/python -O
"myscript.p y")


This works for me:

self.popen = popen2.Popen3(["python", "/usr/local/bin/python",
"-O", "myscript.p y"])

But I don't know if its a hack or a desired feature.

Mathias

Jul 18 '05 #4

Another issue *might* be that the TERM signal is passed to the Pyton
process but ignored on Solaris. Try using a different signal like
SIGQUIT or maybe even SIGKILL. The shell process and all its child
processes should be terminated, by convention on *nix. Also on RedHat
Linux it works.

There may be secondary issue with killing processes created by
popen2.Popen3() . The exit status of the child processes should be
checked to avoid zombies. After killing a child process do call the
wait() or poll() method. The wait() methods will wait forever and may
cause the parent process to hang. To avoid that use poll() in a loop
until the return value is non-negative. The sample code below may
help.

In any case, do use the list format for the cmd. That is better if
there is no compelling need for the intermediate shell process.

/Jean Brouwers
ProphICy Semiconductor, Inc.

PS) Sample code to avoid zombie (on *nix):

<pre>
p = self.popen
os.kill(p.pid, signal.SIGTERM)
t = 2.5 # max wait time in secs
while p.poll() < 0:
if t > 0.5:
t -= 0.25
os.sleep(0.25)
else: # still there, force kill
os.kill(p.pid, signal.SIGKILL)
os.sleep(0.5)
p.poll() # final try
break
</pre>

In article <ma************ *************** **********@pyth on.org>, Bob
Swerdlow <rs*******@tran spose.com> wrote:
My application starts up a number of processes for various purposes using:
self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.p y")
and then shuts them down when appropriate with
os.kill(self.po pen.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 and then
another started by the shell to run my Python script. The os.kill() is
killing the shell process, not the one running my Python code.

Actually, I really want to kill both of these processes, but I only have the
pid for the shell process. I cannot kill the whole process group because
that kills the main process, too (I tried it).

So, what is the best way to kill both the shell process (whose pid is
available from the Popen3 object) and its child process that is running my
Python script? It looks like the python script process id is always one
greater than the shell process id of the shell process, but I'm sure I
cannot rely on that.

Thanks,

Bob Swerdlow
COO
Transpose
rs*******@trans pose.com
207-781-8284
http://www.transpose.com

----------------------------------
Fight Spam!
Add this link to your signature (as I did): http://wecanstopspam.org
Click through to find out more.
----------------------------------

Jul 18 '05 #5
P
Bob Swerdlow wrote:
My application starts up a number of processes for various purposes using:
self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.p y")
and then shuts them down when appropriate with
os.kill(self.po pen.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 and then
another started by the shell to run my Python script. The os.kill() is
killing the shell process, not the one running my Python code.

Actually, I really want to kill both of these processes, but I only have the
pid for the shell process. I cannot kill the whole process group because
that kills the main process, too (I tried it).


Try to get the popen2.Popen3() implementation to create it's own
process group. You can do this by adding an os.setpgrp() call
or maybe you can change the "sh -c" -> "sh -mc"

Pádraig.
Jul 18 '05 #6
P
Bob Swerdlow wrote:
My application starts up a number of processes for various purposes using:
self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.p y")
and then shuts them down when appropriate with
os.kill(self.po pen.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 andthen
another started by the shell to run my Python script. The os.kill() is
killing the shell process, not the one running my Python code.

Actually, I really want to kill both of these processes, but I only have the
pid for the shell process. I cannot kill the whole process group because
that kills the main process, too (I tried it).


Try to get the popen2.Popen3() implementation to create it's own
process group. You can do this by adding an os.setpgrp() call
or maybe you can change the "sh -c" -> "sh -mc"

Pádraig.

Jul 18 '05 #7

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

Similar topics

1
3046
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
2
1580
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 intensive task that has to be repeated many times (with different input parameters). So I've thought to write a Python script that create a child process, wait for it to finish and then starts again another child with different parameters.
1
1904
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 functions control external processes. One function is called with various items governed by the db, does a fork and then execl to from the child to start a new process. Whilst it's not posix i ignore the SIGCHLD signal and let the parent process...
0
1420
by: tiger | last post by:
Hi I was working on a project for school, that deals with processes and windows.....here is a sample of the instruction: You are to implement a game of Nim. Your implementation should consist of three processes. Each process must have a visible window as it runs. The main process is the manager. It should start the other two (player) processes and manage the game. It is responsible for evaluating the game and declaring a winner or a...
0
1333
by: tiger | last post by:
Hi I was working on a project for school, that deals with processes and windows.....here is a sample of the instruction: You are to implement a game of Nim. Your implementation should consist of three processes. Each process must have a visible window as it runs. The main process is the manager. It should start the other two (player) processes and manage the game. It is responsible for evaluating the game and declaring a winner or a...
0
1260
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 => The parent process stays as "Running" until the child process is closed
2
2078
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) in general. Suppose for instance that for some reason or another, after forking several child processes, the main process terminates or gets killed (or segfaults or whatever) and the child processes are orphaned. Is there any way to automatically...
4
4850
by: Ashit Vora | last post by:
Hi, My query is... I have a parent process which forks a child process to perform certain task. I wish to terminate the child process after certain amount of time (say 60 secs). Since I dont wish to leave any zombie process, I need to notify the Child that it has to terminate now. The parent can terminate only after all the child processes have
1
1510
by: umaramnath | last post by:
Hello everyone, I work on Linux for ARM processor for cable modem. There is a tool that I have written (as the job demands) that sends/storms customized UDP packets using raw sockets. I form the packet from scratch so that we have the flexibility to play with different options. This tool is mainly for stress testing routers. The details are here. I actually have multiple interfaces created. Each interface will obtain IP addresses using...
0
9908
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...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9740
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
8744
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...
0
6564
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
5332
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.