summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/powercycle/lib/process_control.py
blob: 309c30f280b78d60497842a75a34a1e7874147fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Wrapper for the ProcessControl class."""
import logging
import psutil

LOGGER = logging.getLogger(__name__)


class ProcessControl(object):
    """Process control class.

    Control processes by name. All matching by supplied name
    pids are controlled.
    """

    def __init__(self, name):
        """Provide 'name' to control the process."""
        self.name = name
        self.pids = []
        self.procs = []

    def get_pids(self):
        """Return list of process ids for process 'self.name'."""
        self.pids = []
        for proc in psutil.process_iter():
            try:
                if proc.name() == self.name:
                    self.pids.append(proc.pid)
            except psutil.NoSuchProcess:
                pass
        return self.pids

    def get_procs(self):
        """Return a list of 'proc' for the associated pids."""
        procs = []
        for pid in self.get_pids():
            try:
                procs.append(psutil.Process(pid))
            except psutil.NoSuchProcess:
                pass
        return procs

    def is_running(self):
        """Return true if any process is running that matches pids."""
        for pid in self.get_pids():
            if psutil.pid_exists(pid):
                return True
        return False

    def kill(self):
        """Kill all running processes that match the list of pids."""
        if self.is_running():
            for proc in self.get_procs():
                try:
                    proc.kill()
                except psutil.NoSuchProcess:
                    LOGGER.info("Could not kill process with pid %d, as it no longer exists",
                                proc.pid)