473,651 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10256
In article
<90************ *************** *******@i76g200 0hsf.googlegrou ps.com>,
bukzor <wo**********@g mail.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**********@g mail.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.readli ne().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.com wrote:
In article
<90ecca29-c4d8-4e89-908a-93850d7de...@i7 6g2000hsf.googl egroups.com>,

bukzor <workithar...@g mail.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...@g mail.comwrote:
On May 21, 12:13 pm, Roy Smith <r...@panix.com wrote:
In article
<90ecca29-c4d8-4e89-908a-93850d7de...@i7 6g2000hsf.googl egroups.com>,
*bukzor <workithar...@g mail.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
3278
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
2139
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 access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I want to send that dataset to another class module. At first it seems that the "object oriented"...
10
1657
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 ***************************************************************************** > void main() ^^^^ Why do so many people post without a) following the newsgroup b) checking the FAQ
13
2489
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 experience. We employ some parts of XP such as pair programming, and this should help. Other than books, does anyone have any suggestions? His skill set is pretty antiquated and we need to get him up to speed as quickly as possible so any suggestions...
0
8277
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8700
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
8465
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
7298
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
4144
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.