Python psutil.process_iter() Examples

The following are 30 code examples of psutil.process_iter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module psutil , or try the search function .
Example #1
Source File: procmon.py    From Paradrop with Apache License 2.0 7 votes vote down vote up
def check(self):
        """
        Check that the service is running and consistent with pid file(s).

        Returns True or False.
        """
        # Set of pids (strings) where the command string matches what we are
        # looking for.
        detected_pids = set()

        # Set of pids (strings) that are both running processes and found in
        # pid files.
        consistent_pids = set()

        # Search for running processes that match our command string.
        for proc in psutil.process_iter():
            try:
                if self.cmdstring in proc.name():
                    detected_pids.add(str(proc.pid))

            # We could also get psutil.ZombieProcess or
            # psutil.AccessDenied.  We want those to be logged.
            except psutil.NoSuchProcess:
                pass

        # Search for pid file(s) and check consistency.
        for pidfile in self.pidfiles:
            for path in glob.iglob(pidfile):
                with open(path, 'r') as source:
                    pid = source.read().strip()

                if pid in detected_pids:
                    consistent_pids.add(pid)
                else:
                    # Delete the stale pid file.
                    os.remove(path)

        return len(consistent_pids) > 0 
Example #2
Source File: test_system.py    From vnpy_crypto with MIT License 7 votes vote down vote up
def test_prcess_iter_w_params(self):
        for p in psutil.process_iter(attrs=['pid']):
            self.assertEqual(list(p.info.keys()), ['pid'])
        with self.assertRaises(ValueError):
            list(psutil.process_iter(attrs=['foo']))
        with mock.patch("psutil._psplatform.Process.cpu_times",
                        side_effect=psutil.AccessDenied(0, "")) as m:
            for p in psutil.process_iter(attrs=["pid", "cpu_times"]):
                self.assertIsNone(p.info['cpu_times'])
                self.assertGreaterEqual(p.info['pid'], 0)
            assert m.called
        with mock.patch("psutil._psplatform.Process.cpu_times",
                        side_effect=psutil.AccessDenied(0, "")) as m:
            flag = object()
            for p in psutil.process_iter(
                    attrs=["pid", "cpu_times"], ad_value=flag):
                self.assertIs(p.info['cpu_times'], flag)
                self.assertGreaterEqual(p.info['pid'], 0)
            assert m.called 
Example #3
Source File: test_process.py    From vnpy_crypto with MIT License 7 votes vote down vote up
def test_ppid(self):
        if hasattr(os, 'getppid'):
            self.assertEqual(psutil.Process().ppid(), os.getppid())
        this_parent = os.getpid()
        sproc = get_test_subprocess()
        p = psutil.Process(sproc.pid)
        self.assertEqual(p.ppid(), this_parent)
        # no other process is supposed to have us as parent
        reap_children(recursive=True)
        if APPVEYOR:
            # Occasional failures, see:
            # https://ci.appveyor.com/project/giampaolo/psutil/build/
            #     job/0hs623nenj7w4m33
            return
        for p in psutil.process_iter():
            if p.pid == sproc.pid:
                continue
            # XXX: sometimes this fails on Windows; not sure why.
            self.assertNotEqual(p.ppid(), this_parent, msg=p) 
Example #4
Source File: redismgmt.py    From monaco with MIT License 6 votes vote down vote up
def verify_instances(self):
        '''
        returns ([instances that should be but aren't running, ...], [instances that shouldn't be but are running, ...])
        '''
        extra_instances = []
        app_pattern = re.compile(r'0\.0\.0\.0:([0-9]*)')
        expected_instances = self.list_instances()
        with self.lock:
            for proc in psutil.process_iter():
                if proc.name() == 'redis-server' and proc.ppid() == 1:
                    match = app_pattern.search(proc.cmdline()[0])
                    if match and match.group(1) != '6379':
                        instance = match.group(1)
                        if not instance in expected_instances:
                            extra_instances.append(instance)
                        elif instance in expected_instances:
                            expected_instances.remove(instance)
        return (expected_instances, extra_instances) 
Example #5
Source File: test_core.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_bash_operator_kill(self):
        import psutil
        sleep_time = "100%d" % os.getpid()
        op = BashOperator(
            task_id='test_bash_operator_kill',
            execution_timeout=timedelta(seconds=1),
            bash_command="/bin/bash -c 'sleep %s'" % sleep_time,
            dag=self.dag)
        self.assertRaises(
            AirflowTaskTimeout,
            op.run,
            start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
        sleep(2)
        pid = -1
        for proc in psutil.process_iter():
            if proc.cmdline() == ['sleep', sleep_time]:
                pid = proc.pid
        if pid != -1:
            os.kill(pid, signal.SIGTERM)
            self.fail("BashOperator's subprocess still running after stopping on timeout!") 
Example #6
Source File: readwrite.py    From scanpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_used_files():
    """Get files used by processes with name scanpy."""
    import psutil

    loop_over_scanpy_processes = (
        proc for proc in psutil.process_iter() if proc.name() == 'scanpy'
    )
    filenames = []
    for proc in loop_over_scanpy_processes:
        try:
            flist = proc.open_files()
            for nt in flist:
                filenames.append(nt.path)
        # This catches a race condition where a process ends
        # before we can examine its files
        except psutil.NoSuchProcess as err:
            pass
    return set(filenames) 
Example #7
Source File: privilege.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def is_up(self):
        """
        Checks if a polkit daemon is running.

        :return: True if it's running, False if it's not.
        :rtype: boolean
        """
        # Note that gnome-shell does not uses a separate process for the
        # polkit-agent, it uses a polkit-agent within its own process so we
        # can't ps-grep a polkit process, we can ps-grep gnome-shell itself.
        if os.getuid() == 0:
            # if you're running as root, it's your problem, not mine.
            return True

        running = False
        for proc in psutil.process_iter():
            if any((pk in proc.name() for pk in polkit.POLKIT_PROC_NAMES)):
                running = True
                break
        return running 
Example #8
Source File: generic.py    From pyiron with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _kill_child(self):
        """
        Internal helper function to kill a child process.
        """
        if not self.server.run_mode.queue and (
            self.status.running or self.status.submitted
        ):
            for proc in psutil.process_iter():
                try:
                    pinfo = proc.as_dict(attrs=["pid", "cwd"])
                except psutil.NoSuchProcess:
                    pass
                else:
                    if pinfo["cwd"] is not None and pinfo["cwd"].startswith(
                        self.working_directory
                    ):
                        job_process = psutil.Process(pinfo["pid"])
                        job_process.kill() 
Example #9
Source File: test_workers_pool.py    From petastorm with Apache License 2.0 6 votes vote down vote up
def test_workers_die_when_main_process_dies(self):
        """ Tests that when the main processes dies, the process workers will kill themselves """
        manager = Manager()
        return_list = manager.list()

        def run_process_pool(return_list):
            pool = ProcessPool(1)
            pool.start(WorkerIdGeneratingWorker)
            return_list.append(pool._workers[0].pid)
            # We dont call pool.stop() and hence leave workers alive

        process = Process(target=run_process_pool, args=(return_list,))
        process.start()
        process.join()
        # The worker has now started

        worker_pid = return_list[0]

        for _ in range(20):
            worker_is_alive = any([p.pid for p in process_iter() if p.pid == worker_pid])
            if not worker_is_alive:
                break
            time.sleep(0.1)
        self.assertFalse(worker_is_alive) 
Example #10
Source File: dtest.py    From cassandra-dtest with Apache License 2.0 6 votes vote down vote up
def kill_windows_cassandra_procs():
    # On Windows, forcefully terminate any leftover previously running cassandra processes. This is a temporary
    # workaround until we can determine the cause of intermittent hung-open tests and file-handles.
    if is_win():
        try:
            import psutil
            for proc in psutil.process_iter():
                try:
                    pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline'])
                except psutil.NoSuchProcess:
                    pass
                else:
                    if (pinfo['name'] == 'java.exe' and '-Dcassandra' in pinfo['cmdline']):
                        print('Found running cassandra process with pid: ' + str(pinfo['pid']) + '. Killing.')
                        psutil.Process(pinfo['pid']).kill()
        except ImportError:
            logger.debug("WARN: psutil not installed. Cannot detect and kill "
                  "running cassandra processes - you may see cascading dtest failures.") 
Example #11
Source File: linux.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def _maybe_get_running_openvpn():
    """
    Looks for previously running openvpn instances.

    :rtype: psutil Process
    """
    openvpn = None
    for p in psutil.process_iter():
        try:
            # This needs more work, see #3268, but for the moment
            # we need to be able to filter out arguments in the form
            # --openvpn-foo, since otherwise we are shooting ourselves
            # in the feet.

            cmdline = p.cmdline()
            if any(map(lambda s: s.find(
                    "LEAPOPENVPN") != -1, cmdline)):
                openvpn = p
                break
        except psutil.AccessDenied:
            pass
    return openvpn 
Example #12
Source File: app2.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def start_app():
    from leap.bitmask.util import STANDALONE
    mypid = os.getpid()

    # Kill a previously-running process
    for proc in psutil.process_iter():
        if proc.name() == PROCNAME and proc.pid != mypid:
            proc.kill()

    # Allow the frozen binary in the bundle double as the cli entrypoint
    # Why have only a user interface when you can have two?

    if STANDALONE and len(sys.argv) > 1:
        if sys.argv[1] == 'bitmask_helpers':
            from leap.bitmask.vpn.helpers import main
            return main()

        from leap.bitmask.cli import bitmask_cli
        return bitmask_cli.main()

    reset_authtoken()
    launch_gui() 
Example #13
Source File: housekeeping.py    From bitmask-dev with GNU General Public License v3.0 6 votes vote down vote up
def check_stale_pidfile():

    def is_pid_running(pidno):
        return 1 == len(
            filter(lambda p: p.pid == int(pidno), psutil.process_iter()))

    pidno = None
    pidfile = os.path.join(get_path_prefix(), 'leap', 'bitmaskd.pid')
    try:
        if os.path.isfile(pidfile):
            with open(pidfile, 'r') as pid_fd:
                pidno = pid_fd.readline().strip()
        if pidno and pidno.isdigit():
            if not is_pid_running(pidno):
                os.unlink(pidfile)
    except Exception as exc:
        print('[bitmask] Error while removing stale file: %r' % exc) 
Example #14
Source File: haproxy_synapse_reaper.py    From synapse-tools with Apache License 2.0 6 votes vote down vote up
def get_alumni(
    username: str,
) -> Iterator[psutil.Process]:
    main_pid = get_main_pid()

    for proc in psutil.process_iter():
        if proc.name() != 'haproxy-synapse':
            continue

        if proc.username() != username:
            continue

        if proc.pid == main_pid:
            continue

        yield proc 
Example #15
Source File: virtual_machine.py    From agentless-system-crawler with Apache License 2.0 6 votes vote down vote up
def __init__(self, name, kernel, distro, arch, host_namespace='',
                 pid=None):
        VirtualMachine.__init__(self, name, kernel, distro, arch,
                                host_namespace=host_namespace)

        if pid is None:
            # Find the pid of the QEMU process running virtual machine `name`
            self.pid = None
            for proc in psutil.process_iter():
                if 'qemu' in proc.name():
                    line = proc.cmdline()
                    if name == line[line.index('-name') + 1]:
                        self.pid = proc.pid

            if self.pid is None:
                raise ValueError('no VM with vm_name: %s' % name)
        else:
            self.pid = pid 
Example #16
Source File: test_process.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_children_duplicates(self):
        # find the process which has the highest number of children
        table = collections.defaultdict(int)
        for p in psutil.process_iter():
            try:
                table[p.ppid()] += 1
            except psutil.Error:
                pass
        # this is the one, now let's make sure there are no duplicates
        pid = sorted(table.items(), key=lambda x: x[1])[-1][0]
        p = psutil.Process(pid)
        try:
            c = p.children(recursive=True)
        except psutil.AccessDenied:  # windows
            pass
        else:
            self.assertEqual(len(c), len(set(c))) 
Example #17
Source File: test_system.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_process_iter(self):
        self.assertIn(os.getpid(), [x.pid for x in psutil.process_iter()])
        sproc = get_test_subprocess()
        self.assertIn(sproc.pid, [x.pid for x in psutil.process_iter()])
        p = psutil.Process(sproc.pid)
        p.kill()
        p.wait()
        self.assertNotIn(sproc.pid, [x.pid for x in psutil.process_iter()])

        with mock.patch('psutil.Process',
                        side_effect=psutil.NoSuchProcess(os.getpid())):
            self.assertEqual(list(psutil.process_iter()), [])
        with mock.patch('psutil.Process',
                        side_effect=psutil.AccessDenied(os.getpid())):
            with self.assertRaises(psutil.AccessDenied):
                list(psutil.process_iter()) 
Example #18
Source File: top.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def poll(interval):
    # sleep some time
    time.sleep(interval)
    procs = []
    procs_status = {}
    for p in psutil.process_iter():
        try:
            p.dict = p.as_dict(['username', 'nice', 'memory_info',
                                'memory_percent', 'cpu_percent',
                                'cpu_times', 'name', 'status'])
            try:
                procs_status[p.dict['status']] += 1
            except KeyError:
                procs_status[p.dict['status']] = 1
        except psutil.NoSuchProcess:
            pass
        else:
            procs.append(p)

    # return processes sorted by CPU percent usage
    processes = sorted(procs, key=lambda p: p.dict['cpu_percent'],
                       reverse=True)
    return (processes, procs_status) 
Example #19
Source File: netstat.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    templ = "%-5s %-30s %-30s %-13s %-6s %s"
    print(templ % (
        "Proto", "Local address", "Remote address", "Status", "PID",
        "Program name"))
    proc_names = {}
    for p in psutil.process_iter(['pid', 'name']):
        proc_names[p.info['pid']] = p.info['name']
    for c in psutil.net_connections(kind='inet'):
        laddr = "%s:%s" % (c.laddr)
        raddr = ""
        if c.raddr:
            raddr = "%s:%s" % (c.raddr)
        print(templ % (
            proto_map[(c.family, c.type)],
            laddr,
            raddr or AD,
            c.status,
            c.pid or AD,
            proc_names.get(c.pid, '?')[:15],
        )) 
Example #20
Source File: process_handler.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def get_runtime_snapshot():
  """Return a list of current processes and their command lines as string."""
  process_strings = []
  for process in psutil.process_iter():
    try:
      process_info = process.as_dict(attrs=['name', 'cmdline', 'pid', 'ppid'])
      process_string = '{name} ({pid}, {ppid})'.format(
          name=process_info['name'],
          pid=process_info['pid'],
          ppid=process_info['ppid'])
      process_cmd_line = process_info['cmdline']
      if process_cmd_line:
        process_string += ': {cmd_line}'.format(
            cmd_line=(' '.join(process_cmd_line)))
      process_strings.append(process_string)
    except (psutil.AccessDenied, psutil.NoSuchProcess, OSError):
      # Ignore the error, use whatever info is available for access.
      pass

  return '\n'.join(sorted(process_strings)) 
Example #21
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_prcess_iter_w_attrs(self):
        for p in psutil.process_iter(attrs=['pid']):
            self.assertEqual(list(p.info.keys()), ['pid'])
        with self.assertRaises(ValueError):
            list(psutil.process_iter(attrs=['foo']))
        with mock.patch("psutil._psplatform.Process.cpu_times",
                        side_effect=psutil.AccessDenied(0, "")) as m:
            for p in psutil.process_iter(attrs=["pid", "cpu_times"]):
                self.assertIsNone(p.info['cpu_times'])
                self.assertGreaterEqual(p.info['pid'], 0)
            assert m.called
        with mock.patch("psutil._psplatform.Process.cpu_times",
                        side_effect=psutil.AccessDenied(0, "")) as m:
            flag = object()
            for p in psutil.process_iter(
                    attrs=["pid", "cpu_times"], ad_value=flag):
                self.assertIs(p.info['cpu_times'], flag)
                self.assertGreaterEqual(p.info['pid'], 0)
            assert m.called 
Example #22
Source File: test_system.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_process_iter(self):
        self.assertIn(os.getpid(), [x.pid for x in psutil.process_iter()])
        sproc = self.spawn_testproc()
        self.assertIn(sproc.pid, [x.pid for x in psutil.process_iter()])
        p = psutil.Process(sproc.pid)
        p.kill()
        p.wait()
        self.assertNotIn(sproc.pid, [x.pid for x in psutil.process_iter()])

        with mock.patch('psutil.Process',
                        side_effect=psutil.NoSuchProcess(os.getpid())):
            self.assertEqual(list(psutil.process_iter()), [])
        with mock.patch('psutil.Process',
                        side_effect=psutil.AccessDenied(os.getpid())):
            with self.assertRaises(psutil.AccessDenied):
                list(psutil.process_iter()) 
Example #23
Source File: test_process.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_children_duplicates(self):
        # find the process which has the highest number of children
        table = collections.defaultdict(int)
        for p in psutil.process_iter():
            try:
                table[p.ppid()] += 1
            except psutil.Error:
                pass
        # this is the one, now let's make sure there are no duplicates
        pid = sorted(table.items(), key=lambda x: x[1])[-1][0]
        if LINUX and pid == 0:
            raise self.skipTest("PID 0")
        p = psutil.Process(pid)
        try:
            c = p.children(recursive=True)
        except psutil.AccessDenied:  # windows
            pass
        else:
            self.assertEqual(len(c), len(set(c))) 
Example #24
Source File: connection_utils.py    From agentless-system-crawler with Apache License 2.0 6 votes vote down vote up
def crawl_connections():
    created_since = -1

    proc_list = psutil.process_iter()

    for p in proc_list:
        pid = (p.pid() if hasattr(p.pid, '__call__') else p.pid)
        status = (p.status() if hasattr(p.status, '__call__'
                                        ) else p.status)
        if status == psutil.STATUS_ZOMBIE:
            continue

        create_time = (
            p.create_time() if hasattr(
                p.create_time,
                '__call__') else p.create_time)
        name = (p.name() if hasattr(p.name, '__call__') else p.name)

        if create_time <= created_since:
            continue
        for conn in p.get_connections():
            yield crawl_single_connection(conn, pid, name) 
Example #25
Source File: pstree.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main():
    # construct a dict where 'values' are all the processes
    # having 'key' as their parent
    tree = collections.defaultdict(list)
    for p in psutil.process_iter():
        try:
            tree[p.ppid()].append(p.pid)
        except (psutil.NoSuchProcess, psutil.ZombieProcess):
            pass
    # on systems supporting PID 0, PID 0's parent is usually 0
    if 0 in tree and 0 in tree[0]:
        tree[0].remove(0)
    print_tree(min(tree), tree) 
Example #26
Source File: test_standard_task_runner.py    From airflow with Apache License 2.0 5 votes vote down vote up
def _procs_in_pgroup(pgid):
        for proc in psutil.process_iter(attrs=['pid', 'name']):
            try:
                if os.getpgid(proc.pid) == pgid and proc.pid != 0:
                    yield proc
            except OSError:
                pass 
Example #27
Source File: botgui.py    From PUBG-Battlepoint-FarmBot with GNU General Public License v3.0 5 votes vote down vote up
def closeIt(self):
        for proc in process_iter():
            # check whether the process name matches
            if proc.name() == self.PROCNAME:
                proc.kill()
                _exit(1)
        QCoreApplication.instance().quit 
Example #28
Source File: procsmem.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main():
    ad_pids = []
    procs = []
    for p in psutil.process_iter():
        with p.oneshot():
            try:
                mem = p.memory_full_info()
                info = p.as_dict(["cmdline", "username"])
            except psutil.AccessDenied:
                ad_pids.append(p.pid)
            except psutil.NoSuchProcess:
                pass
            else:
                p._uss = mem.uss
                p._rss = mem.rss
                if not p._uss:
                    continue
                p._pss = getattr(mem, "pss", "")
                p._swap = getattr(mem, "swap", "")
                p._info = info
                procs.append(p)

    procs.sort(key=lambda p: p._uss)
    templ = "%-7s %-7s %-30s %7s %7s %7s %7s"
    print(templ % ("PID", "User", "Cmdline", "USS", "PSS", "Swap", "RSS"))
    print("=" * 78)
    for p in procs[:86]:
        line = templ % (
            p.pid,
            p._info["username"][:7] if p._info["username"] else "",
            " ".join(p._info["cmdline"])[:30],
            convert_bytes(p._uss),
            convert_bytes(p._pss) if p._pss != "" else "",
            convert_bytes(p._swap) if p._swap != "" else "",
            convert_bytes(p._rss),
        )
        print(line)
    if ad_pids:
        print("warning: access denied for %s pids" % (len(ad_pids)),
              file=sys.stderr) 
Example #29
Source File: pidof.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def pidof(pgname):
    pids = []
    for proc in psutil.process_iter(['name', 'cmdline']):
        # search for matches in the process name and cmdline
        if proc.info['name'] == pgname or \
                proc.info['cmdline'] and proc.info['cmdline'][0] == pgname:
            pids.append(str(proc.pid))
    return pids 
Example #30
Source File: test_kill_service_at_exit.py    From selenium-python-helium with MIT License 5 votes vote down vote up
def _get_running_processes(self, image_names):
		result = []
		for p in psutil.process_iter():
			if p.name in image_names:
				result.append(p)
		return result