os.kill........

Dave Cinege dcinege at psychosis.com
Wed Nov 21 02:57:39 EST 2001


On Tuesday 20 November 2001 12:05, Martin Franklin wrote:
> Hi,
>
> I'm looking for a way of determining if a process is still running (having
> spawned it from a python app), I have come up with os.kill(PID, 0)
> this will raise an exception if the process does not exist, and do nothing
> if the process does exist.  The sig of zero does not _seem_ to affect it
> (at least not on my linux box)
>
> My question is.....  is this an OK way of doing this?  do I have any other
> _pure_ python options.....
>
>
> Regards
> Martin

Probably not very portable, but this may help. It's a function I use
to see if a lockfile is stale or not. It's grab info from the process
info in /proc. (Yes the function could be cleaner...)


def processorisactive(tmpdir):	# This is very linux

	for lf in glob.glob('%s/lockfile.*' % (tmpdir)):
		pid = string.split(os.path.basename(lf), '.')[1]

		try:
			pid_file = '/proc/%s/status' % (pid)
			f = open(pid_file, 'r')
		except:
			continue	# Process does not exist
	
		name = string.split(f.readline())[1]
		state = string.split(f.readline())[1]
		f.close()	

		# FIX ME Check for Sleep state correct?
		if name[:4] == 'mss_':
			if state == 'R' or state == 'D' or state == 'S': # It's a running mss_X processor
				mss.log("\tActive lockfile in '%s'. Skipping." % (tmpdir))
				return 1

		mss.log("\tRemoving stale lockfile '%s/%s'." % (tmpdir,lf))
		os.remove(lf)		# Remove stale lockfile		
				
	return 0


-- 
The time is now 22:37 (Totalitarian)  -  http://www.ccops.org/




More information about the Python-list mailing list