Skip to content

Commit

Permalink
Merge pull request #3983 from Slancaster1/get_pids_for
Browse files Browse the repository at this point in the history
utils_sys.py: Add function for finding process PIDS
  • Loading branch information
Yingshun authored Sep 6, 2024
2 parents 37e94de + 2dbe925 commit e1d7d52
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions virttest/utils_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,33 @@ def get_host_bridge_id(session=None):

host_bridges = re.findall(hostbridge_regex, output)
return host_bridges if host_bridges else []


def get_pids_for(process_names, sort_pids=True, session=None):
"""
Given a list of names, retrieve the PIDs for
matching processes. Sort of equivalent
to: 'ps aux | grep name'
:param process_names: List of process names to look for
"""

status, ps_cmd = cmd_status_output("ps aux", shell=True, session=session)
if status != 0 or not ps_cmd:
return []

ps_output = ps_cmd.split("\n")
relevant_procs = [
proc
for proc in ps_output
for wanted_name in process_names
if wanted_name in proc
]

relevant_procs = [line.split() for line in relevant_procs]
relevant_pids = [int(proc[1]) for proc in relevant_procs]

if sort_pids:
relevant_pids.sort()

return relevant_pids

0 comments on commit e1d7d52

Please sign in to comment.