annotate python/process.py @ 180:f52486ceadee

handle defunct processes
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 08 Nov 2011 17:11:35 -0800
parents a5061b41a781
children f9863f733344
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 import os
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 import shlex
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import subprocess
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 import sys
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
179
a5061b41a781 make flag optional
Jeff Hammel <jhammel@mozilla.com>
parents: 178
diff changeset
6 def ps(arg='axwww'):
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
7 """
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
8 python front-end to `ps`
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
9 http://en.wikipedia.org/wiki/Ps_%28Unix%29
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
10 returns a list of process dicts based on the `ps` header
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
11 """
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 retval = []
179
a5061b41a781 make flag optional
Jeff Hammel <jhammel@mozilla.com>
parents: 178
diff changeset
13 process = subprocess.Popen(['ps', arg], stdout=subprocess.PIPE)
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 stdout, _ = process.communicate()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 header = None
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 for line in stdout.splitlines():
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 line = line.strip()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 if header is None:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 # first line is the header
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 header = line.split()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 continue
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 split = line.split(None, len(header)-1)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 process_dict = dict(zip(header, split))
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 retval.append(process_dict)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 return retval
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
27 def running_processes(name, defunct=True):
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 """
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 returns a list of
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 {'PID': PID of process (int)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 'command': command line of process (list)}
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
32 with the executable named `name`.
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
33 - defunct: whether to return defunct processes
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 """
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 retval = []
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 for process in ps():
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 command = process['COMMAND']
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 command = shlex.split(command)
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
39 if command[-1] == '<defunct>':
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
40 command = command[:-1]
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
41 if not command or not defunct:
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
42 continue
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 prog = command[0]
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 basename = os.path.basename(prog)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 if basename == name:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 retval.append((int(process['PID']), command))
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 return retval
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 if __name__ == '__main__':
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 for arg in sys.argv[1:]:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 processes = running_processes(arg)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 for pid, command in processes:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 print '%s %s : %s' % (pid, arg, command)