Entry
List of pids running a program
Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 163, Tim Evans
"""
Packages: operating_systems.unix
"""
"""
> [Adrian Eyre]
> > os.system("ps aux|grep someProcess > pids.temp")
>
> Try:
> f = os.popen("ps aux|grep " + someProcessStr)
> l = f.readlines()
> f.close()
The pipe to grep is slightly unnecessary, you could just use python to
filter the lines. Try this:
"""
#=============== ps.py ===============
#!/usr/bin/python
import os
import string
def pids(program):
'''Return a list of process ids for all processes that are running
"program". Relies on a particular output format for ps a little
too much.'''
result = []
f = os.popen('ps aux', 'r')
for l in f.readlines():
fields = string.split(l)
if fields[10] == program:
result.append(int(fields[1]))
return result
if __name__ == '__main__':
print pids('gnome-terminal')
#==============================
#$ ./ps.py
#[18910, 18925]