Mercurial > hg > config
annotate python/process.py @ 370:4198a58cc520
adding aliases
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 21 Jul 2013 05:35:28 -0700 |
parents | 748c232c0af3 |
children | 581436742074 |
rev | line source |
---|---|
178 | 1 import os |
2 import shlex | |
3 import subprocess | |
4 import sys | |
5 | |
179 | 6 def ps(arg='axwww'): |
180 | 7 """ |
8 python front-end to `ps` | |
9 http://en.wikipedia.org/wiki/Ps_%28Unix%29 | |
10 returns a list of process dicts based on the `ps` header | |
11 """ | |
178 | 12 retval = [] |
179 | 13 process = subprocess.Popen(['ps', arg], stdout=subprocess.PIPE) |
178 | 14 stdout, _ = process.communicate() |
15 header = None | |
16 for line in stdout.splitlines(): | |
17 line = line.strip() | |
18 if header is None: | |
19 # first line is the header | |
20 header = line.split() | |
21 continue | |
22 split = line.split(None, len(header)-1) | |
23 process_dict = dict(zip(header, split)) | |
24 retval.append(process_dict) | |
25 return retval | |
26 | |
183
748c232c0af3
update to process examiner
Jeff Hammel <jhammel@mozilla.com>
parents:
182
diff
changeset
|
27 def running_processes(name, psarg='axwww', defunct=True): |
178 | 28 """ |
182 | 29 returns a list of 2-tuples of running processes: |
30 (pid, ['path/to/executable', 'args', '...']) | |
180 | 31 with the executable named `name`. |
32 - defunct: whether to return defunct processes | |
178 | 33 """ |
34 retval = [] | |
183
748c232c0af3
update to process examiner
Jeff Hammel <jhammel@mozilla.com>
parents:
182
diff
changeset
|
35 for process in ps(psarg): |
178 | 36 command = process['COMMAND'] |
37 command = shlex.split(command) | |
180 | 38 if command[-1] == '<defunct>': |
39 command = command[:-1] | |
40 if not command or not defunct: | |
41 continue | |
178 | 42 prog = command[0] |
43 basename = os.path.basename(prog) | |
44 if basename == name: | |
45 retval.append((int(process['PID']), command)) | |
46 return retval | |
47 | |
48 if __name__ == '__main__': | |
49 for arg in sys.argv[1:]: | |
50 processes = running_processes(arg) | |
51 for pid, command in processes: | |
52 print '%s %s : %s' % (pid, arg, command) |