diff --git a/src/cloudmesh/common/Shell.py b/src/cloudmesh/common/Shell.py index fccc6b1..75c702d 100755 --- a/src/cloudmesh/common/Shell.py +++ b/src/cloudmesh/common/Shell.py @@ -579,40 +579,6 @@ def oneline(script, seperator=" && "): """ return seperator.join(textwrap.dedent(script).strip().splitlines()) - def find_process(name): - """ find a process by name - - :param name: the name of the process - :return: A list of dicts in which the attributes pid, command, - and created are available and the name matches - the specified name argument. - - TODO: at one point this should be moved to cloudmesh.common - - Return a list of processes matching 'name'. - """ - - processes = None - for p in psutil.process_iter(): - found = None - try: - found = p.name() - except (psutil.AccessDenied, psutil.ZombieProcess): - pass - except psutil.NoSuchProcess: - continue - if name == found: - if processes is None: - processes = [] - processes.append( - { - "pid": p.pid, - "command": " ".join(p.cmdline()), - "created": p.create_time() - }) - return processes - - @staticmethod def is_choco_installed(): """return true if chocolatey windows package manager is installed @@ -1367,6 +1333,43 @@ def keystone(cls, *args): """ return cls.execute("keystone", args) + @staticmethod + def find_process(name): + """ + Find a process by name. + + Args: + name (str): The name of the process. + + Returns: + list: A list of dictionaries containing the attributes 'pid', 'command', + and 'created' for each process that matches the specified name. + + Example: + >>> find_process("python") + [{'pid': 1234, 'command': 'python script.py', 'created': 1634567890}] + """ + processes = None + for p in psutil.process_iter(): + found = None + try: + found = p.name() + except (psutil.AccessDenied, psutil.ZombieProcess): + pass + except psutil.NoSuchProcess: + continue + if name == found: + if processes is None: + processes = [] + processes.append( + { + "pid": p.pid, + "command": " ".join(p.cmdline()), + "created": p.create_time(), + } + ) + return processes + @staticmethod def kill_pid(pid): """