From 86fcc11ae1e428ca5f3438102b5f9093f54903a0 Mon Sep 17 00:00:00 2001 From: Kajetan Puchalski Date: Tue, 4 Jul 2023 17:28:40 +0100 Subject: [PATCH] target: Add is_running() Add the "is_running" function that can be used to check if a given process is running on the target device. It will return True if a process matching the name is found and Falsa otherwise. Signed-off-by: Kajetan Puchalski --- devlib/target.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/devlib/target.py b/devlib/target.py index 8624aab..f4c5ebd 100644 --- a/devlib/target.py +++ b/devlib/target.py @@ -54,7 +54,8 @@ from devlib.platform import Platform from devlib.exception import (DevlibTransientError, TargetStableError, TargetNotRespondingError, TimeoutError, TargetTransientError, KernelConfigKeyError, - TargetError, HostError, TargetCalledProcessError) # pylint: disable=redefined-builtin + TargetError, HostError, TargetCalledProcessError, + TargetStableCalledProcessError) # pylint: disable=redefined-builtin from devlib.utils.ssh import SshConnection from devlib.utils.android import AdbConnection, AndroidProperties, LogcatMonitor, adb_command, adb_disconnect, INTENT_FLAGS from devlib.utils.misc import memoized, isiterable, convert_new_lines, groupby_value @@ -279,6 +280,12 @@ class Target(object): self._setup_shutils() return self._shutils + def is_running(self, comm): + cmd_ps = f'''{self.busybox} ps -A -T -o stat,comm''' + cmd_awk = f'''{self.busybox} awk 'BEGIN{{found=0}} {{state=$1; $1=""; if ($state != "Z" && $0 == " {comm}") {{found=1}}}} END {{print found}}' ''' + result = self.execute(f"{cmd_ps} | {cmd_awk}") + return bool(int(result)) + @tls_property def _conn(self): try: