pids: Search for process name matches in the cmdline (#59318)

* Also search for process name matches in the cmdline
* Return integer pids
* Use correct variable name
* Fix linter concerns
This commit is contained in:
Will Medlar 2019-09-20 01:30:25 -05:00 committed by Abhijeet Kasurde
parent 41bfd2bf0e
commit 6541bb0315

View file

@ -52,8 +52,23 @@ except ImportError:
HAS_PSUTIL = False HAS_PSUTIL = False
def compare_lower(a, b):
if a is None or b is None:
# this could just be "return False" but would lead to surprising behavior if both a and b are None
return a == b
return a.lower() == b.lower()
def get_pid(name): def get_pid(name):
return [p.info['pid'] for p in psutil.process_iter(attrs=['pid', 'name']) if p.info and p.info.get('name', None) and p.info['name'].lower() == name.lower()] pids = []
for proc in psutil.process_iter(attrs=['name', 'cmdline']):
if compare_lower(proc.info['name'], name) or \
proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name):
pids.append(proc.pid)
return pids
def main(): def main():