473,549 Members | 2,758 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 7034

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
3035
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
1555
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,...
1
1886
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...
0
1407
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)...
0
1324
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)...
0
1240
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
2064
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...
4
4835
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...
1
1501
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...
0
7444
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
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. ...
1
7467
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...
0
6039
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...
1
5367
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...
0
3497
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...
1
1932
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
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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...

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.