Python psutil.NoSuchProcess() Examples

The following are 30 code examples of psutil.NoSuchProcess(). 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: _common.py    From ALF with Apache License 2.0 8 votes vote down vote up
def __init__(self, pid, idle_limit=None, memory_limit=None, time_limit=None):
        self.limit = {"idle":idle_limit,
                      "memory":memory_limit,
                      "time":time_limit}
        self.check = {"idle":0,
                      "memory":0,
                      "time":0}
        self.idle_start = None
        try:
            if isinstance(pid, psutil.Process):
                self.ps = pid
            else:
                self.ps = psutil.Process(pid)
            self.ps.get_cpu_percent(interval=0)
        except psutil.NoSuchProcess:
            self.ps = None 
Example #2
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 #3
Source File: test_process.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_kill_terminate(self):
        # subprocess.Popen()'s terminate(), kill() and send_signal() do
        # not raise exception after the process is gone. psutil.Popen
        # diverges from that.
        cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
        with psutil.Popen(cmd, stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE) as proc:
            proc.terminate()
            proc.wait()
            self.assertRaises(psutil.NoSuchProcess, proc.terminate)
            self.assertRaises(psutil.NoSuchProcess, proc.kill)
            self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
                              signal.SIGTERM)
            if WINDOWS and sys.version_info >= (2, 7):
                self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
                                  signal.CTRL_C_EVENT)
                self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
                                  signal.CTRL_BREAK_EVENT) 
Example #4
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_procfs_path(self):
        tdir = self.get_testfn()
        os.mkdir(tdir)
        try:
            psutil.PROCFS_PATH = tdir
            self.assertRaises(IOError, psutil.virtual_memory)
            self.assertRaises(IOError, psutil.cpu_times)
            self.assertRaises(IOError, psutil.cpu_times, percpu=True)
            self.assertRaises(IOError, psutil.boot_time)
            # self.assertRaises(IOError, psutil.pids)
            self.assertRaises(IOError, psutil.net_connections)
            self.assertRaises(IOError, psutil.net_io_counters)
            self.assertRaises(IOError, psutil.net_if_stats)
            # self.assertRaises(IOError, psutil.disk_io_counters)
            self.assertRaises(IOError, psutil.disk_partitions)
            self.assertRaises(psutil.NoSuchProcess, psutil.Process)
        finally:
            psutil.PROCFS_PATH = "/proc" 
Example #5
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 #6
Source File: WinDBGMemoryLimit.py    From ALF with Apache License 2.0 6 votes vote down vote up
def get_mem_usage(pid):
    try:
        proc = psutil.Process(pid)
        tmp_val = proc.get_memory_info()[0]
    except psutil.NoSuchProcess:
        tmp_val = 0
    return tmp_val 
Example #7
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 #8
Source File: _common.py    From ALF with Apache License 2.0 6 votes vote down vote up
def check_memory(self):
        now = prof_timer()
        if self.limit["memory"] and self.ps and (now - self.check["memory"]) > self.MEMORY_CHECK:
            self.check["memory"] = now
            try:
                target_mem = self.ps.get_memory_info()[0] # target memory usage
                for child in self.ps.get_children(recursive=True):
                    try:
                        target_mem += child.get_memory_info()[0]
                    except psutil.NoSuchProcess:
                        pass
            except psutil.NoSuchProcess:
                target_mem = 0
            if target_mem > self.limit["memory"]:
                return True
        return False 
Example #9
Source File: endpoint.py    From funcX with Apache License 2.0 6 votes vote down vote up
def check_pidfile(filepath, match_name, endpoint_name):
    """ Helper function to identify possible dead endpoints
    """
    if not os.path.exists(filepath):
        return

    older_pid = int(open(filepath, 'r').read().strip())

    try:
        proc = psutil.Process(older_pid)
        if proc.name() == match_name:
            logger.info("Endpoint is already active")
    except psutil.NoSuchProcess:
        logger.info("A prior Endpoint instance appears to have been terminated without proper cleanup")
        logger.info('''Please cleanup using:
    $ funcx-endpoint stop {}'''.format(endpoint_name)) 
Example #10
Source File: _common.py    From ALF with Apache License 2.0 6 votes vote down vote up
def check_idle(self):
        now = prof_timer()
        if self.limit["idle"] and self.ps and (now - self.check["idle"]) > self.IDLE_CHECK:
            self.check["idle"] = now
            try:
                cpu_time = self.ps.get_cpu_percent(interval=0) # target cpu usage
                for child in self.ps.get_children(recursive=True):
                    try:
                        c_cpu = child.get_cpu_percent(interval=0)
                        if c_cpu > cpu_time:
                            cpu_time = c_cpu
                    except psutil.NoSuchProcess:
                        pass
            except psutil.NoSuchProcess:
                return False
            if cpu_time < self.IDLE_THRESHOLD:
                if self.idle_start and (now - self.idle_start) > self.limit["idle"]:
                    return True
                if self.idle_start is None:
                    self.idle_start = now
            else:
                self.idle_start = None
        return False 
Example #11
Source File: utils.py    From mars with Apache License 2.0 6 votes vote down vote up
def kill_process_tree(pid, include_parent=True):
    try:
        import psutil
    except ImportError:  # pragma: no cover
        return
    try:
        proc = psutil.Process(pid)
    except psutil.NoSuchProcess:
        return

    plasma_sock_dir = None
    children = proc.children(recursive=True)
    if include_parent:
        children.append(proc)
    for p in children:
        try:
            if 'plasma' in p.name():
                plasma_sock_dir = next((conn.laddr for conn in p.connections('unix')
                                        if 'plasma' in conn.laddr), None)
            p.kill()
        except psutil.NoSuchProcess:  # pragma: no cover
            pass
    if plasma_sock_dir:
        shutil.rmtree(plasma_sock_dir, ignore_errors=True) 
Example #12
Source File: cluster.py    From pyquarkchain with MIT License 6 votes vote down vote up
def kill_child_processes(parent_pid):
    """ Kill all the subprocesses recursively """
    try:
        parent = psutil.Process(parent_pid)
    except psutil.NoSuchProcess:
        return
    children = parent.children(recursive=True)
    print(
        "================================ SHUTTING DOWN CLUSTER ================================"
    )
    for process in children:
        try:
            print("SIGTERM >>> " + " ".join(process.cmdline()[1:]))
        except Exception:
            pass
        process.send_signal(signal.SIGTERM)
        process.wait() 
Example #13
Source File: jobs.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def _get_job_status(self, jobpid, jobhash):
        try:
            jobprocess = psutil.Process(pid=jobpid)

        except psutil.NoSuchProcess:
            return {
                'status': 'DONE',
                'returncode': None
            }

        if hash(jobprocess) != jobhash:
            return {
                'status': 'DONE',
                'returncode': None
            }

        return {
            'status': 'RUNNING'
        } 
Example #14
Source File: test.py    From px with MIT License 6 votes vote down vote up
def runPxTest(cmd, testproc, ips, port, proxy):
    global PROXY
    PROXY = proxy

    pipe = subprocess.Popen("cmd /k start /wait /min " + cmd + " --port=" + str(port), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    ret = testproc(ips, port)

    pxproc = psutil.Process(pipe.pid)
    for child in pxproc.children(recursive=True):
        try:
            child.kill()
        except psutil.NoSuchProcess:
            pass
    try:
        pxproc.kill()
    except:
        pass

    time.sleep(0.5)

    return ret 
Example #15
Source File: views.py    From passhport with GNU Affero General Public License v3.0 6 votes vote down vote up
def sshdisconnection(pid):
    """Kill the pid no matter what"""
    try:
        parent = psutil.Process(int(pid))
        for child in parent.children(): 
            child.kill()
        parent.kill()

    except Exception as E:
        if type(E) == psutil.NoSuchProcess:
            app.logger.warning("Impossible to kill: no such process with PID " + str(pid))

    return "Done"


########################
#### FILES DOWNLOAD #### 
Example #16
Source File: nfp_process.py    From nightmare with GNU General Public License v2.0 6 votes vote down vote up
def check_cpu(self):
    while True:
      try:
        if self.pid is None:
          time.sleep(0.2)
          continue

        proc = psutil.Process(self.pid)
        cpu = 0
        l = []
        for x in xrange(20):
          tmp = int(proc.cpu_percent(interval=0.1))
          cpu += tmp
          l.append(tmp)

        if cpu is not None and (cpu <= 100 or l.count(0) > 10):
          log("CPU at 0%, killing")
          self.cpu_killed = True
          self.do_kill()
          break
        else:
          time.sleep(0.5)
      except psutil.NoSuchProcess:
        break 
Example #17
Source File: test_process.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_kill_terminate(self):
        # subprocess.Popen()'s terminate(), kill() and send_signal() do
        # not raise exception after the process is gone. psutil.Popen
        # diverges from that.
        cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
        with psutil.Popen(cmd, stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE) as proc:
            proc.terminate()
            proc.wait()
            self.assertRaises(psutil.NoSuchProcess, proc.terminate)
            self.assertRaises(psutil.NoSuchProcess, proc.kill)
            self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
                              signal.SIGTERM)
            if WINDOWS and sys.version_info >= (2, 7):
                self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
                                  signal.CTRL_C_EVENT)
                self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
                                  signal.CTRL_BREAK_EVENT) 
Example #18
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 #19
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 #20
Source File: new_process.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def kill_process_tree(root_pid):
  """Kill process tree."""
  try:
    parent = psutil.Process(root_pid)
    children = parent.children(recursive=True)
  except (psutil.AccessDenied, psutil.NoSuchProcess, OSError):
    logs.log_warn('Failed to find or access process.')
    return

  for child in children:
    try:
      child.kill()
    except (psutil.AccessDenied, psutil.NoSuchProcess, OSError):
      logs.log_warn('Failed to kill process child.')

  try:
    parent.kill()
  except (psutil.AccessDenied, psutil.NoSuchProcess, OSError):
    logs.log_warn('Failed to kill process.') 
Example #21
Source File: utils.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def get_process_ids(process_id, recursive=True):
  """Return list of pids for a process and its descendants."""
  # Try to find the running process.
  if not psutil.pid_exists(process_id):
    return []

  pids = [process_id]

  try:
    psutil_handle = psutil.Process(process_id)
    children = psutil_handle.children(recursive=recursive)
    for child in children:
      pids.append(child.pid)
  except psutil.NoSuchProcess:
    # Avoid too much logging when the process already died.
    return []

  except (psutil.AccessDenied, OSError):
    logs.log_warn('Failed to get process children.')
    return []

  return pids 
Example #22
Source File: train.py    From FlexTensor with MIT License 5 votes vote down vote up
def kill_child_processes(parent_pid, sig=signal.SIGTERM):
    """kill all child processes recursively"""
    try:
        parent = psutil.Process(parent_pid)
    except psutil.NoSuchProcess:
        return
    children = parent.children(recursive=True)
    for process in children:
        try:
            process.send_signal(sig)
        except psutil.NoSuchProcess:
            return 
Example #23
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 #24
Source File: scheduler.py    From FlexTensor with MIT License 5 votes vote down vote up
def kill_child_processes(parent_pid, sig=signal.SIGTERM):
    """kill all child processes recursively"""
    try:
        parent = psutil.Process(parent_pid)
    except psutil.NoSuchProcess:
        return
    children = parent.children(recursive=True)
    for process in children:
        try:
            process.send_signal(sig)
        except psutil.NoSuchProcess:
            return 
Example #25
Source File: test_linux.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_issue_1014(self):
        # Emulates a case where smaps file does not exist. In this case
        # wrap_exception decorator should not raise NoSuchProcess.
        with mock_open_exception(
                '/proc/%s/smaps' % os.getpid(),
                IOError(errno.ENOENT, "")) as m:
            p = psutil.Process()
            with self.assertRaises(IOError) as err:
                p.memory_maps()
            self.assertEqual(err.exception.errno, errno.ENOENT)
            assert m.called 
Example #26
Source File: process_handler.py    From clusterfuzz with Apache License 2.0 5 votes vote down vote up
def terminate_process(process_id, kill=False):
  """Terminates a process by its process id."""
  try:
    process = psutil.Process(process_id)

    if kill:
      process.kill()
    else:
      process.terminate()

  except (psutil.AccessDenied, psutil.NoSuchProcess, OSError):
    logs.log_warn('Failed to terminate process.') 
Example #27
Source File: pykd_iface.py    From nightmare with GNU General Public License v2.0 5 votes vote down vote up
def check_cpu(self):
    while True:
      try:
        if self.pid is None:
          time.sleep(0.2)
          continue

        proc = psutil.Process(self.pid)
        if proc is None:
          break

        cpu = 0
        l = []
        for x in xrange(20):
          tmp = int(proc.cpu_percent(interval=0.1))
          cpu += tmp
          l.append(tmp)

        if cpu is not None and (cpu <= 100 or l.count(0) > 10):
          log("CPU at 0%, killing")
          self.do_stop = True
          pykd.breakin()
          break
        else:
          time.sleep(0.5)
      except psutil.NoSuchProcess:
        self.do_stop = True
        break 
Example #28
Source File: base.py    From flyingcloud with Apache License 2.0 5 votes vote down vote up
def find_port_forwarding(self, namespace, args):
        for process in psutil.process_iter():
            try:
                if process.name().endswith('ssh'):
                    if process.cmdline()[-len(args):] == args:
                        return process
            except psutil.NoSuchProcess:
                pass 
Example #29
Source File: test_linux.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_issue_1014(self):
        # Emulates a case where smaps file does not exist. In this case
        # wrap_exception decorator should not raise NoSuchProcess.
        with mock_open_exception(
                '/proc/%s/smaps' % os.getpid(),
                IOError(errno.ENOENT, "")) as m:
            p = psutil.Process()
            with self.assertRaises(FileNotFoundError):
                p.memory_maps()
            assert m.called 
Example #30
Source File: test_misc.py    From psutil with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_process__repr__(self, func=repr):
        p = psutil.Process(self.spawn_testproc().pid)
        r = func(p)
        self.assertIn("psutil.Process", r)
        self.assertIn("pid=%s" % p.pid, r)
        self.assertIn("name='%s'" % p.name(), r)
        self.assertIn("status=", r)
        self.assertNotIn("exitcode=", r)
        p.terminate()
        p.wait()
        r = func(p)
        self.assertIn("status='terminated'", r)
        self.assertIn("exitcode=", r)

        with mock.patch.object(psutil.Process, "name",
                               side_effect=psutil.ZombieProcess(os.getpid())):
            p = psutil.Process()
            r = func(p)
            self.assertIn("pid=%s" % p.pid, r)
            self.assertIn("status='zombie'", r)
            self.assertNotIn("name=", r)
        with mock.patch.object(psutil.Process, "name",
                               side_effect=psutil.NoSuchProcess(os.getpid())):
            p = psutil.Process()
            r = func(p)
            self.assertIn("pid=%s" % p.pid, r)
            self.assertIn("terminated", r)
            self.assertNotIn("name=", r)
        with mock.patch.object(psutil.Process, "name",
                               side_effect=psutil.AccessDenied(os.getpid())):
            p = psutil.Process()
            r = func(p)
            self.assertIn("pid=%s" % p.pid, r)
            self.assertNotIn("name=", r)