# HG changeset patch # User Jeff Hammel # Date 1320801095 28800 # Node ID f52486ceadee7ce3723cdabb09def1e985262780 # Parent a5061b41a781eb503e122892b18a08d541d54645 handle defunct processes diff -r a5061b41a781 -r f52486ceadee python/process.py --- a/python/process.py Tue Nov 08 15:36:55 2011 -0800 +++ b/python/process.py Tue Nov 08 17:11:35 2011 -0800 @@ -4,6 +4,11 @@ import sys def ps(arg='axwww'): + """ + python front-end to `ps` + http://en.wikipedia.org/wiki/Ps_%28Unix%29 + returns a list of process dicts based on the `ps` header + """ retval = [] process = subprocess.Popen(['ps', arg], stdout=subprocess.PIPE) stdout, _ = process.communicate() @@ -19,17 +24,22 @@ retval.append(process_dict) return retval -def running_processes(name): +def running_processes(name, defunct=True): """ returns a list of {'PID': PID of process (int) 'command': command line of process (list)} - with the executable named `name` + with the executable named `name`. + - defunct: whether to return defunct processes """ retval = [] for process in ps(): command = process['COMMAND'] command = shlex.split(command) + if command[-1] == '': + command = command[:-1] + if not command or not defunct: + continue prog = command[0] basename = os.path.basename(prog) if basename == name: