472,989 Members | 3,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

how to kill a process

Hi Fellows,
I have a problem with process termination. I have a python code that
apache runs through a django interface.
The code is very simple, first, it creates a process with the
subprocess.Popen call, and afterwards, (using a web request) the
python code uses the PID of the previously created process(stored in a
db) and kills it with an os.kill call using the SIGKILL signal.

The creation of the process is ok, apache calls the python code, this
code creates the process and exits leaving the process up and
running :)
But when the python code is called to kill the created process, the
process is left in a zombie state.

The kill code that I'm using is:
os.kill(pid, signal.SIGKILL)

and I also tried:
kill_proc = Popen("kill -9 " + pid, shell=true)
but with no success.

I suppose that the reason maybe that the python code exits before the
kill call has finished,
so I tried with a while loop until kill_proc.poll() != None, but
without success too :(

do you know what is what I'm doing wrong?

thanks very much.-

Jun 12 '07 #1
4 20708
Richard Rossel wrote:
Hi Fellows,
I have a problem with process termination. I have a python code that
apache runs through a django interface.
The code is very simple, first, it creates a process with the
subprocess.Popen call, and afterwards, (using a web request) the
python code uses the PID of the previously created process(stored in a
db) and kills it with an os.kill call using the SIGKILL signal.

The creation of the process is ok, apache calls the python code, this
code creates the process and exits leaving the process up and
running :)
But when the python code is called to kill the created process, the
process is left in a zombie state.

The kill code that I'm using is:
os.kill(pid, signal.SIGKILL)

and I also tried:
kill_proc = Popen("kill -9 " + pid, shell=true)
but with no success.

I suppose that the reason maybe that the python code exits before the
kill call has finished,
so I tried with a while loop until kill_proc.poll() != None, but
without success too :(

do you know what is what I'm doing wrong?

thanks very much.-
Wouldn't it be better to make the process that you start respond gracefully
to some signal? Either a command over a socket or some other signal?

-Larry
Jun 12 '07 #2
On 6/12/07, Richard Rossel <he*******@gmail.comwrote:
But when the python code is called to kill the created process, the
process is left in a zombie state.
If the process is left in a zombie state, it's because the parent
process isn't calling wait(2). If the parent process is your own
python script, you might try a call to os.wait after the kill
statement.

--
Evan Klitzke <ev**@yelp.com>
Jun 12 '07 #3
On 12 jun, 13:24, "Evan Klitzke" <e...@yelp.comwrote:
On 6/12/07, Richard Rossel <henhis...@gmail.comwrote:
But when the python code is called to kill the created process, the
process is left in a zombie state.

If the process is left in a zombie state, it's because the parent
process isn't calling wait(2). If the parent process is your own
python script, you might try a call to os.wait after the kill
statement.
The wait call did the trick, but now a sh from kill process
left in zombie state, so afterwards the waitpid, I added a code line
to call poll() method from the kill process, and doesn't generates
zombie
process anymore :)

Thanks for your helps
Jun 12 '07 #4
Richard Rossel <he*******@gmail.comwrote:
Hi Fellows,
I have a problem with process termination. I have a python code that
apache runs through a django interface.
The code is very simple, first, it creates a process with the
subprocess.Popen call, and afterwards, (using a web request) the
python code uses the PID of the previously created process(stored in a
db) and kills it with an os.kill call using the SIGKILL signal.
The creation of the process is ok, apache calls the python code, this
code creates the process and exits leaving the process up and
running :)
But when the python code is called to kill the created process, the
process is left in a zombie state.
A zombie is an entry in the process table that stores the exit value
of a deceased process. (The word is a bit of a misnomer ... and
the better term would be "death certificate").

You want to do an os.wait() to clear that entry.

(The process is well and truly dead after this sort of os.kill()
but the system still wants the parent process to be able to retrieve
the exit value and this is the Unix/Linux mechanism for storing that).
The kill code that I'm using is:
os.kill(pid, signal.SIGKILL)
and I also tried:
kill_proc = Popen("kill -9 " + pid, shell=true)
but with no success.
The misunderstanding here is that you *were* successful.
You have killed the process. It's dead. Nothing's left but
a death certificate (or "gravestone" or "corpse" or whatever
you want to call it). All that remains is for the parent to
drop by the morgue (the process table) and pick up the remains.
I suppose that the reason maybe that the python code exits before the
kill call has finished,
so I tried with a while loop until kill_proc.poll() != None, but
without success too :(
do you know what is what I'm doing wrong?
You are fundamentally misunderstanding the nature of the process
table and the meaning of "zombie." Don't feel bad. It's a very
common misunderstanding which has sadly been very poorly addressed
by books on Unix systems administration and programming.
thanks very much.-
Glad to help. Try this:

os.kill(pid, signal.SIGKILL)
killedpid, stat = os.waitpid(pid, os.WNOHANG)
if killedpid == 0:
print >sys.stderr, "ACK! PROCESS NOT KILLED?"

... I'm using the "WNOHANG" flag here so that the os.waitpid()
function will return a tuple of (0,0) if the process isn't
dead yet. (Shouldn't be possible under these circumstances, but
understanding how to do this in non-blocking mode is better than
using the same code pattern in some other case and then being
surprised, probably unpleasantly, when your process is blocked
by the call).
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

Jun 13 '07 #5

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

Similar topics

5
by: Brett | last post by:
How do I gracefully kill a process than restart it? The process Image Name is known. Thanks, Brett
4
by: iwdu15 | last post by:
hi, im been working on a process manager for awhile and i cant find anywhere how to kill a running process....any help would b awsome -- -iwdu15
3
by: Sehboo | last post by:
Hi, Is it possible that you can monitor your memory usage and kill some process when memory usage reaches 80% or so? I have an application which uses lot of memory and then system hangs, but...
4
by: drodrig | last post by:
Hi. I am trying to close/kill all processes that show visible windows on Windows XP. So far I've created a script that uses win32gui.EnumWindows to iterate through all windows, check for which...
2
by: eddie69 | last post by:
Hi, I am working in an application (VB 2005) that has a listview of all running processes and hastheability to close (kill) any active process. So far, I have been able to kill any process at...
0
by: vinitfichadia | last post by:
Hello friends, In my vb.net application i would like to kill the process using the application name i.e, i m dynamically opening grid data into excel inthe same window and excel filename is date,...
10
by: fadel daher | last post by:
i am building an application to monitor servers. i have written code that gets me all running processes on remote server, but i cant manage to get a code to kill a remote process . can any one...
0
by: Sergei Shelukhin | last post by:
Hi. I am writing an app that updates a certain windows service that is had previously installed. It stops the service, rewrites the file, and starts it again. On some machines, however, even...
5
by: Guillaume Dargaud | last post by:
Hello all, I can launch an external program using system() or the exec() family of calls. But I'm under the impression that I then can't kill that process using standard C functions. kill() is...
2
by: news.microsoft.com | last post by:
How can a VB program detect a kill process which was used to close it down. I have a routine that detects when a user exits the program and I can track the session times. But how can I detect when...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.