3
# This Source Code Form is subject to the terms of the Mozilla Public
4
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
# You can obtain one at http://mozilla.org/MPL/2.0/.
13
# determine the platform-specific invocation of `ps`
23
python front-end to `ps`
24
http://en.wikipedia.org/wiki/Ps_%28Unix%29
25
returns a list of process dicts based on the `ps` header
28
process = subprocess.Popen(['ps', arg], stdout=subprocess.PIPE)
29
stdout, _ = process.communicate()
31
for line in stdout.splitlines():
34
# first line is the header
37
split = line.split(None, len(header)-1)
38
process_dict = dict(zip(header, split))
39
retval.append(process_dict)
42
def running_processes(name, psarg=psarg, defunct=True):
45
{'PID': PID of process (int)
46
'command': command line of process (list)}
47
with the executable named `name`.
48
- defunct: whether to return defunct processes
51
for process in ps(psarg):
52
command = process['COMMAND']
53
command = shlex.split(command)
54
if command[-1] == '<defunct>':
55
command = command[:-1]
56
if not command or not defunct:
58
if 'STAT' in process and not defunct:
59
if process['STAT'] == 'Z+':
62
basename = os.path.basename(prog)
64
retval.append((int(process['PID']), command))
68
"""Get all the pids matching name"""
71
# use the windows-specific implementation
73
return wpk.get_pids(name)
75
return [pid for pid,_ in running_processes(name)]
77
if __name__ == '__main__':
79
for i in sys.argv[1:]:
80
pids.update(get_pids(i))
81
for i in sorted(pids):