178
|
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)
|