Programmatically Getting Process ID
Patrick Phalen
python-list at teleo.net
Fri Mar 3 19:29:55 EST 2000
[Samuel A. Falvo II, on Fri, 03 Mar 2000]
:: Is there a way that I can programmatically obtain a process ID for a
:: particularly named process? For example, I'd like to be able to do this:
::
:: pid = get_pid_of( "radiusd" )
:: kill( pid, SIGHUP )
::
:: What would get_pid_of() be defined as?
How about:
def get_pid_of(name):
thispid = os.getpid() # in case script has the search string in its name
cmd = "%s%s" % ('ps -ef | grep ', name)
procs = os.popen(cmd).readlines()
for line in procs:
g = string.split(line)
try:
pid = g[1]
except IndexError:
print 'oops'
raise SystemExit
else:
pid = int(pid)
if pid == thispid:
pass
else:
os.kill(pid, SIGHUP)
figuring out how to keep from delivering an error to stdout for child
processes is left as an exercise for those less lazy than I
More information about the Python-list
mailing list