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 # Support for both BSD and UNIX syntax
53 # `ps aux` returns COMMAND, `ps -ef` returns CMD
55 command
= process
['COMMAND']
57 command
= process
['CMD']
59 command
= shlex
.split(command
)
60 if command
[-1] == '<defunct>':
61 command
= command
[:-1]
62 if not command
or not defunct
:
64 if 'STAT' in process
and not defunct
:
65 if process
['STAT'] == 'Z+':
68 basename
= os
.path
.basename(prog
)
70 retval
.append((int(process
['PID']), command
))
74 """Get all the pids matching name"""
77 # use the windows-specific implementation
79 return wpk
.get_pids(name
)
81 return [pid
for pid
,_
in running_processes(name
)]
83 if __name__
== '__main__':
85 for i
in sys
.argv
[1:]:
86 pids
.update(get_pids(i
))
87 for i
in sorted(pids
):