473,287 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

best way to check if pid is dead?

Does anyone have a pythonic way to check if a process is dead, given
the pid?

This is the function I'm using is quite OS dependent. A good candidate
might be "try: kill(pid)", since it throws an exception if the pid is
dead, but that sends a signal which might interfere with the process.

Thanks.
--Buck
Jun 27 '08 #1
4 10188
In article
<90**********************************@i76g2000hsf. googlegroups.com>,
bukzor <wo**********@gmail.comwrote:
Does anyone have a pythonic way to check if a process is dead, given
the pid?

This is the function I'm using is quite OS dependent. A good candidate
might be "try: kill(pid)", since it throws an exception if the pid is
dead, but that sends a signal which might interfere with the process.

Thanks.
--Buck
The canonical way is to do kill(pid, 0). If it doesn't throw, the process
exists. No actual signal is sent to the process either way.

Of course, the process could exit immediately after the kill() call, so by
the time you find out it's alive, it's dead. Such is life.
Jun 27 '08 #2
On Wed, May 21, 2008 at 3:02 PM, bukzor <wo**********@gmail.comwrote:
Does anyone have a pythonic way to check if a process is dead, given
the pid?

This is the function I'm using is quite OS dependent. A good candidate
might be "try: kill(pid)", since it throws an exception if the pid is
dead, but that sends a signal which might interfere with the process.

Thanks.
--Buck
--
http://mail.python.org/mailman/listinfo/python-list
I don't know if you would call this pythonic, but the way I do it in linux is:

import os
os.path.exists("/proc/%d"%(pid))

Or, more to the point, I'm usually checking to see if processes I
forked have finished, without just having to do a wait4 on them; in
the case you can do something like

procfile = open("/proc/%d/stat" %(pid))
procfile.readline().split[2]

You can do man proc to see what each of the possible letters means; I
look for Z to find that the process has exited but it's waiting for
its parent to do a wait4.

HTH
-dan
Jun 27 '08 #3
On May 21, 12:13 pm, Roy Smith <r...@panix.comwrote:
In article
<90ecca29-c4d8-4e89-908a-93850d7de...@i76g2000hsf.googlegroups.com>,

bukzor <workithar...@gmail.comwrote:
Does anyone have a pythonic way to check if a process is dead, given
the pid?
This is the function I'm using is quite OS dependent. A good candidate
might be "try: kill(pid)", since it throws an exception if the pid is
dead, but that sends a signal which might interfere with the process.
Thanks.
--Buck

The canonical way is to do kill(pid, 0). If it doesn't throw, the process
exists. No actual signal is sent to the process either way.

Of course, the process could exit immediately after the kill() call, so by
the time you find out it's alive, it's dead. Such is life.
Thanks! That's exactly what I was looking for. A little more
background:

"If sig is 0 (the null signal), error checking is performed but no
signal is actually sent. The null signal can be used to check the
validity of pid."

Taken from : http://www.opengroup.org/onlinepubs/...ions/kill.html
Jun 27 '08 #4
On May 21, 1:27*pm, bukzor <workithar...@gmail.comwrote:
On May 21, 12:13 pm, Roy Smith <r...@panix.comwrote:
In article
<90ecca29-c4d8-4e89-908a-93850d7de...@i76g2000hsf.googlegroups.com>,
*bukzor <workithar...@gmail.comwrote:
Does anyone have a pythonic way to check if a process is dead, given
the pid?
This is the function I'm using is quite OS dependent. A good candidate
might be "try: kill(pid)", since it throws an exception if the pid is
dead, but that sends a signal which might interfere with the process.
Thanks.
--Buck
The canonical way is to do kill(pid, 0). *If it doesn't throw, the process
exists. *No actual signal is sent to the process either way.
Of course, the process could exit immediately after the kill() call, so by
the time you find out it's alive, it's dead. *Such is life.

Thanks! That's exactly what I was looking for. A little more
background:

"If sig is 0 (the null signal), error checking is performed but no
signal is actually sent. The null signal can be used to check the
validity of pid."

Taken from :http://www.opengroup.org/onlinepubs/...ions/kill.html

Here are the functions I wrote with this information. There are three
functions:
kill() is similar to os.kill, but returns True if the pid is dead and
throws less exceptions
dead() checks if a process is dead, and gets rid of zombies if
necessary
goodkill() kills a pid by sending gradually harser signals until dead.


def kill(pid, signal=0):
"""sends a signal to a process
returns True if the pid is dead
with no signal argument, sends no signal"""
#if 'ps --no-headers' returns no lines, the pid is dead
from os import kill
try: return kill(pid, signal)
except OSError, e:
#process is dead
if e.errno == 3: return True
#no permissions
elif e.errno == 1: return False
else: raise

def dead(pid):
if kill(pid): return True

#maybe the pid is a zombie that needs us to wait4 it
from os import waitpid, WNOHANG
try: dead = waitpid(pid, WNOHANG)[0]
except OSError, e:
#pid is not a child
if e.errno == 10: return False
else: raise
return dead

#def kill(pid, sig=0): pass #DEBUG: test hang condition
def goodkill(pid, interval=1, hung=20):
"let process die gracefully, gradually send harsher signals if
necessary"
from signal import SIGTERM, SIGINT, SIGHUP, SIGKILL
from time import sleep

for signal in [SIGTERM, SIGINT, SIGHUP]:
if kill(pid, signal): return
if dead(pid): return
sleep(interval)

i = 0
while True:
#infinite-loop protection
if i < hung: i += 1
else:
print "Process %s is hung. Giving up kill." % pid
return
if kill(pid, SIGKILL): return
if dead(pid): return
sleep(interval)
Jun 27 '08 #5

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

Similar topics

1
by: Jay | last post by:
Hi ! I am a beginner in PHP. Does anybody know how to check if an URL is alive or dead in PHP (not exist or server down...or whatever ) ? Any help would be appreciated ! Thanks ! Jay
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
10
by: rahul8143 | last post by:
Hello sir, First sorry if seen this as again posted due to network problem. Thanks for pointing me that void main() is wrong to use...
13
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.