Mercurial > hg > config
comparison python/process.py @ 178:fa94f0b4459d
add python ps
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Tue, 08 Nov 2011 12:08:11 -0800 |
| parents | |
| children | a5061b41a781 |
comparison
equal
deleted
inserted
replaced
| 177:6ae566bb1a99 | 178:fa94f0b4459d |
|---|---|
| 1 import os | |
| 2 import shlex | |
| 3 import subprocess | |
| 4 import sys | |
| 5 | |
| 6 def ps(): | |
| 7 retval = [] | |
| 8 process = subprocess.Popen(['ps', 'axwww'], stdout=subprocess.PIPE) | |
| 9 stdout, _ = process.communicate() | |
| 10 header = None | |
| 11 for line in stdout.splitlines(): | |
| 12 line = line.strip() | |
| 13 if header is None: | |
| 14 # first line is the header | |
| 15 header = line.split() | |
| 16 continue | |
| 17 split = line.split(None, len(header)-1) | |
| 18 process_dict = dict(zip(header, split)) | |
| 19 retval.append(process_dict) | |
| 20 return retval | |
| 21 | |
| 22 def running_processes(name): | |
| 23 """ | |
| 24 returns a list of | |
| 25 {'PID': PID of process (int) | |
| 26 'command': command line of process (list)} | |
| 27 with the executable named `name` | |
| 28 """ | |
| 29 retval = [] | |
| 30 for process in ps(): | |
| 31 command = process['COMMAND'] | |
| 32 command = shlex.split(command) | |
| 33 prog = command[0] | |
| 34 basename = os.path.basename(prog) | |
| 35 if basename == name: | |
| 36 retval.append((int(process['PID']), command)) | |
| 37 return retval | |
| 38 | |
| 39 if __name__ == '__main__': | |
| 40 for arg in sys.argv[1:]: | |
| 41 processes = running_processes(arg) | |
| 42 for pid, command in processes: | |
| 43 print '%s %s : %s' % (pid, arg, command) |
