Skip to content

Commit

Permalink
utils_sys.py: Add function for finding process PIDS
Browse files Browse the repository at this point in the history
Added small utility function get_pids_for(<list>). This
function reads the output of 'ps aux' and gets the pids
for any process in <list>.

Signed-off-by: Slancaster1 <[email protected]>
  • Loading branch information
Slancaster1 committed Aug 29, 2024
1 parent 3ff9f2d commit 553b61b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions virttest/utils_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,30 @@ 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, 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]
relevant_pids.sort()

return relevant_pids

0 comments on commit 553b61b

Please sign in to comment.