From 5164ceb5c89a755260b023d6ccd8431146ba9067 Mon Sep 17 00:00:00 2001 From: Praveen K Pandey Date: Tue, 14 May 2024 18:42:02 +0530 Subject: [PATCH 1/2] Added a library function which retruns linux process based on process name Added a library function which retruns linux process based on process name Signed-off-by: Praveen K Pandey --- avocado/utils/linux.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/avocado/utils/linux.py b/avocado/utils/linux.py index dcca20dc46..a14aad77f8 100644 --- a/avocado/utils/linux.py +++ b/avocado/utils/linux.py @@ -24,6 +24,7 @@ """ import os +import psutil from avocado.utils import genio @@ -72,3 +73,17 @@ def enable_selinux_enforcing(): if is_selinux_enforcing(): return True return False + + +def get_processes_by_name(name): + """ + Return a list of processes matching 'name' + + :param name: name of the process + + :return: list of processes + """ + matching_processes = [ + proc for proc in psutil.process_iter(["name"]) if proc.info["name"] == name + ] + return matching_processes From 229187f8d36a5bf1e0514e60952191ee31268f98 Mon Sep 17 00:00:00 2001 From: Praveen K Pandey Date: Tue, 14 May 2024 18:47:36 +0530 Subject: [PATCH 2/2] added a library function which help to kill linuxprocess added a library function which help to kill linuxprocess Signed-off-by: Praveen K Pandey --- avocado/utils/linux.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/avocado/utils/linux.py b/avocado/utils/linux.py index a14aad77f8..3c0fc826a5 100644 --- a/avocado/utils/linux.py +++ b/avocado/utils/linux.py @@ -22,12 +22,15 @@ """ Linux OS utilities """ - +import logging import os + import psutil from avocado.utils import genio +LOGGER = logging.getLogger(__name__) + def get_proc_sys(key): """ @@ -87,3 +90,18 @@ def get_processes_by_name(name): proc for proc in psutil.process_iter(["name"]) if proc.info["name"] == name ] return matching_processes + + +def kill_processes(processes): + """ + Attempt to kill all 'processes' + + :param processes: linux process + """ + for proc in processes: + try: + proc.kill() + except psutil.NoSuchProcess: + LOGGER.error(f"No such process: {proc.pid}") + except psutil.AccessDenied: + LOGGER.error(f"Access denied to {proc.pid}")