Skip to content

Commit

Permalink
move find_pid closer to pid related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
laszewsk committed Jan 8, 2024
1 parent 806ab2a commit 61a0a2f
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions src/cloudmesh/common/Shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 61a0a2f

Please sign in to comment.