Programmatically Getting Process ID
Patrick Phalen
python-list at teleo.net
Fri Mar 3 19:29:55 EST 2000
More information about the Python-list mailing list
Fri Mar 3 19:29:55 EST 2000
- Previous message (by thread): comp.lang.python: more traffic than ever before
- Next message (by thread): Programmatically Getting Process ID
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[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
- Previous message (by thread): comp.lang.python: more traffic than ever before
- Next message (by thread): Programmatically Getting Process ID
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list