Пример #1
0
def reap_children(search_all=False):
    """Kill any subprocess started by this test suite and ensure that
    no zombies stick around to hog resources and create problems when
    looking for refleaks.
    """
    global _subprocesses_started
    procs = _subprocesses_started.copy()
    if search_all:
        this_process = psutil.Process()
        for p in this_process.children(recursive=True):
            procs.add(p)
    for p in procs:
        try:
            p.terminate()
        except psutil.NoSuchProcess:
            pass
    gone, alive = psutil.wait_procs(procs, timeout=GLOBAL_TIMEOUT)
    for p in alive:
        warn("couldn't terminate process %s" % p)
        try:
            p.kill()
        except psutil.NoSuchProcess:
            pass
    _, alive = psutil.wait_procs(alive, timeout=GLOBAL_TIMEOUT)
    if alive:
        warn("couldn't not kill processes %s" % str(alive))
    _subprocesses_started = set(alive)
def matar_procesos(pid):
    parent = psutil.Process(pid)
    children = parent.get_children(recursive=True)

    for child in children:
        child.kill()
    psutil.wait_procs(children, timeout=5)
Пример #3
0
 def _abort(self):
     """
     Terminate all live jobs.
     """
     for i, (slot, proc) in enumerate(zip(self._slots, self._procs)):
         if slot:
             if not is_windows:
                 try:
                     os.killpg(proc.pid, signal.SIGTERM)
                 except OSError:
                     # If the process with pid did not have time to become a process group leader,
                     # then pgid does not exist and os.killpg could not kill the process,
                     # so re-try kill the process only.
                     try:
                         os.kill(proc.pid, signal.SIGTERM)
                     except OSError:
                         pass
             else:
                 root_proc = psutil.Process(proc.pid)
                 children = root_proc.children(recursive=True) + [root_proc]
                 for child_proc in children:
                     try:
                         # Would be easier to use proc.terminate() here but psutils
                         # (up to version 5.4.0) on Windows terminates processes with
                         # the 0 signal/code, making the outcome of the terminated
                         # process indistinguishable from a successful execution.
                         os.kill(child_proc.pid, signal.SIGTERM)
                     except OSError:
                         pass
                 psutil.wait_procs(children, timeout=1)
         self._slots[i], self._procs[i] = 0, None
Пример #4
0
    def terminate_all_children():
        """ Terminates all children """
        logger.debug("Terminating Process...")
        print("Terminating Process...", flush=True)
        children = psutil.Process().children(recursive=True)
        for child in children:
            child.terminate()
        _, alive = psutil.wait_procs(children, timeout=10)
        if not alive:
            logger.debug("Terminated")
            print("Terminated")
            return

        logger.debug("Termination timed out. Killing Process...")
        print("Termination timed out. Killing Process...", flush=True)
        for child in alive:
            child.kill()
        _, alive = psutil.wait_procs(alive, timeout=10)
        if not alive:
            logger.debug("Killed")
            print("Killed")
        else:
            for child in alive:
                msg = "Process {} survived SIGKILL. Giving up".format(child)
                logger.debug(msg)
                print(msg)
Пример #5
0
    def end(self):
        """
        Terminate (and then kill) the manager process launched.
        :return:
        """
        if not self._process:
            self.log.warn('Ending without manager process.')
            return
        this_process = psutil.Process(os.getpid())
        try:
            manager_process = psutil.Process(self._process.pid)
        except psutil.NoSuchProcess:
            self.log.info("Manager process not running.")
            return

        # First try SIGTERM
        if manager_process.is_running() \
                and manager_process.pid in [x.pid for x in this_process.children()]:
            self.log.info("Terminating manager process: %s", manager_process.pid)
            manager_process.terminate()
            # TODO: Remove magic number
            timeout = 5
            self.log.info("Waiting up to %ss for manager process to exit...", timeout)
            try:
                psutil.wait_procs({manager_process}, timeout)
            except psutil.TimeoutExpired:
                self.log.debug("Ran out of time while waiting for "
                               "processes to exit")

        # Then SIGKILL
        if manager_process.is_running() \
                and manager_process.pid in [x.pid for x in this_process.children()]:
            self.log.info("Killing manager process: %s", manager_process.pid)
            manager_process.kill()
            manager_process.wait()
Пример #6
0
    def close(self):
        """Close SSH tunnel."""
        logging.debug('Closing tunnel "%s"', self.context.uri)

        if self._closed:
            return

        # Find all ssh instances for user with uri tunnel the hard way...
        targets = [
            p
            for p in psutil.process_iter(attrs=['name', 'username', 'cmdline'])
            if p.info['username'] == getpass.getuser()
            and p.info['name'] == 'ssh'
            and self.context.local_socket in ' '.join(p.info['cmdline'])
        ]  # yapf: disable

        # ask nicely for ssh to quit, reap results
        for proc in targets:
            proc.terminate()
        _, alive = psutil.wait_procs(targets, timeout=300)

        # kill off the uncooperative, then report any stragglers
        for proc in alive:
            proc.kill()
        _, alive = psutil.wait_procs(targets, timeout=300)

        for proc in alive:
            logging.info('process %d survived SIGKILL, giving up.', proc.pid)

        with suppress(OSError):
            os.remove(self.context.local_socket)
        self._closed = True
Пример #7
0
Файл: core.py Проект: owtf/owtf
    def kill_children(self, parent_pid, sig=signal.SIGINT):
        """Kill all OWTF child process when the SIGINT is received

        :param parent_pid: The pid of the parent OWTF process
        :type parent_pid: `int`
        :param sig: Signal received
        :type sig: `int`
        :return:
        :rtype: None
        """
        def on_terminate(proc):
            """Log debug info on child process termination
            
            :param proc: Process pid
            :rtype: None
            """
            logging.debug("Process {} terminated with exit code {}".format(proc, proc.returncode))

        parent = psutil.Process(parent_pid)
        children = parent.children(recursive=True)
        for child in children:
            child.send_signal(sig)

        _, alive = psutil.wait_procs(children, callback=on_terminate)
        if not alive:
            # send SIGKILL
            for pid in alive:
                logging.debug("Process {} survived SIGTERM; trying SIGKILL" % pid)
                pid.kill()
        _, alive = psutil.wait_procs(alive, callback=on_terminate)
        if not alive:
            # give up
            for pid in alive:
                logging.debug("Process {} survived SIGKILL; giving up" % pid)
Пример #8
0
def reap(process, timeout=3):
    "Tries hard to terminate and ultimately kill all the children of this process."
    def on_terminate(proc):
        print("process {} terminated with exit code {}".format(proc.pid, proc.returncode))

    try:
        procs = process.children(recursive=True)
        # send SIGTERM
        for p in procs:
            print("Killing ", p.pid)
            p.terminate()
        gone, alive = psutil.wait_procs(procs, timeout=timeout, callback=on_terminate)
        if alive:
            # send SIGKILL
            for p in alive:
                print("process {} survived SIGTERM; trying SIGKILL" % p.pid)
                p.kill()
            gone, alive = psutil.wait_procs(alive, timeout=timeout, callback=on_terminate)
            if alive:
                # give up
                for p in alive:
                    print("process {} survived SIGKILL; giving up" % p.pid)

        print("Killing ", process.pid)
        process.kill()
    except:
        print("Killing failed; assuming process exited early.")
Пример #9
0
    def stop(self):
        """Stop the daemon."""
        pid = self.get_pid()

        if os.path.exists(self.pid_file):
            os.remove(self.pid_file)

        if pid is None:
            return
        elif psutil.pid_exists(pid):
            parent = psutil.Process(pid)
            if parent.name() == self.pid_name:
                procs = parent.children(recursive=True)
                procs.append(parent)
                for p in procs:
                    p.terminate()
                gone, alive = psutil.wait_procs(procs, timeout=3)
                if alive:
                    for p in alive:
                        p.kill()
                gone, alive = psutil.wait_procs(alive, timeout=3)
                if alive:
                    for p in alive:
                        message = "Process {0} survived SIGKILL"
                        sys.stderr.write(message.format(p))
Пример #10
0
def kill_process_tree(logger, pid, timeout=DEFAULT_TIME_TO_WAIT_AFTER_SIGTERM):
    """
    TODO(saguziel): also kill the root process after killing descendants
  
    Kills the process's descendants. Kills using the `kill`
    shell command so that it can change users. Note: killing via PIDs
    has the potential to the wrong process if the process dies and the
    PID gets recycled in a narrow time window.

    :param logger: logger
    :type logger: logging.Logger
    """
    try:
        root_process = psutil.Process(pid)
    except psutil.NoSuchProcess:
        logger.warning("PID: {} does not exist".format(pid))
        return

    # Check child processes to reduce cases where a child process died but
    # the PID got reused.
    descendant_processes = [x for x in root_process.children(recursive=True)
                            if x.is_running()]

    if len(descendant_processes) != 0:
        logger.info("Terminating descendant processes of {} PID: {}"
                    .format(root_process.cmdline(),
                            root_process.pid))
        temp_processes = descendant_processes[:]
        for descendant in temp_processes:
            logger.info("Terminating descendant process {} PID: {}"
                        .format(descendant.cmdline(), descendant.pid))
            if not kill_using_shell(logger, descendant.pid, signal.SIGTERM):
                descendant_processes.remove(descendant)

        logger.info("Waiting up to {}s for processes to exit..."
                    .format(timeout))
        try:
            psutil.wait_procs(descendant_processes, timeout)
            logger.info("Done waiting")
        except psutil.TimeoutExpired:
            logger.warning("Ran out of time while waiting for "
                           "processes to exit")
        # Then SIGKILL
        descendant_processes = [x for x in root_process.children(recursive=True)
                                if x.is_running()]

        if len(descendant_processes) > 0:
            temp_processes = descendant_processes[:]
            for descendant in temp_processes:
                logger.info("Killing descendant process {} PID: {}"
                            .format(descendant.cmdline(), descendant.pid))
                if not kill_using_shell(logger, descendant.pid, signal.SIGKILL):
                    descendant_processes.remove(descendant)
                else:
                    descendant.wait()
            logger.info("Killed all descendant processes of {} PID: {}"
                        .format(root_process.cmdline(),
                                root_process.pid))
    else:
        logger.debug("There are no descendant processes to kill")
Пример #11
0
def reap_children(recursive=False):
    """Terminate and wait() any subprocess started by this test suite
    and ensure that no zombies stick around to hog resources and
    create problems  when looking for refleaks.

    If resursive is True it also tries to terminate and wait()
    all grandchildren started by this process.
    """
    # Get the children here, before terminating the children sub
    # processes as we don't want to lose the intermediate reference
    # in case of grandchildren.
    if recursive:
        children = psutil.Process().children(recursive=True)
    else:
        children = []

    # Terminate subprocess.Popen instances "cleanly" by closing their
    # fds and wiat()ing for them in order to avoid zombies.
    subprocs = _subprocesses_started.copy()
    _subprocesses_started.clear()
    for subp in subprocs:
        try:
            subp.terminate()
        except OSError as err:
            if err.errno != errno.ESRCH:
                raise
        if subp.stdout:
            subp.stdout.close()
        if subp.stderr:
            subp.stderr.close()
        try:
            # Flushing a BufferedWriter may raise an error.
            if subp.stdin:
                subp.stdin.close()
        finally:
            # Wait for the process to terminate, to avoid zombies.
            try:
                subp.wait()
            except OSError as err:
                if err.errno != errno.ECHILD:
                    raise

    # Terminates grandchildren.
    if children:
        for p in children:
            try:
                p.terminate()
            except psutil.NoSuchProcess:
                pass
        gone, alive = psutil.wait_procs(children, timeout=GLOBAL_TIMEOUT)
        for p in alive:
            warn("couldn't terminate process %r; attempting kill()" % p)
            try:
                p.kill()
            except psutil.NoSuchProcess:
                pass
        _, alive = psutil.wait_procs(alive, timeout=GLOBAL_TIMEOUT)
        if alive:
            for p in alive:
                warn("process %r survived kill()" % p)
Пример #12
0
 def watch_process(self):
     """
         Watcher thread.
         This one relaunches airodump eatch time it dies until
         we call stop()
     """
     psutil.wait_procs([psutil.Process(self._proc.pid)],
                       callback=self.start)
Пример #13
0
	def run(self):
		running=True;
		cpu = [];
		mem = [];
		x = [];
		z = []
		d = 0.0;
		p = self.proc;
		i0 = []; 
		i1 = [];
		i2 = [];
		i3 = [];
		try:
			while d<self.totaltime:
				d += 1;
				if p.status!='zombie':
					# If the program is running, this captures
					# the vital-statistics.
					cpu.append(p.get_cpu_percent());
					mem.append(p.get_memory_percent());
					z.append(p.get_cpu_times().system)
					x.append(d)
					i0.append([p.get_io_counters()[0]])
					i1.append([p.get_io_counters()[1]])
					i2.append([p.get_io_counters()[2]])
					i3.append([p.get_io_counters()[3]])
				else:
					# This watches and ensures that the
					# process is indeed dead. This is the first
					# level of exception handling and
					# loop-breakage. 
					procs = psutil.get_process_list();
					gone, alive = psutil.wait_procs(procs, 3, callback=on_terminate)
					break
				time.sleep(1)
		except psutil._error.AccessDenied:
			# This exception watches for the natural death of the
			# process. This is the second level of redundancy handling
			# the death of the task, the first one is in the else
			# statement above. 
			p.kill()
			print "It has died and has become a... "+p.status;
			procs = psutil.get_process_list();
			gone, alive = psutil.wait_procs(procs, 3, callback=on_terminate)
		except KeyboardInterrupt:
			# This exception allows the user to exit the watch. You
			# have to terminate the process elsewhere. 
			print "Exiting..."+p.status
			procs = psutil.get_process_list();
			gone, alive = psutil.wait_procs(procs, 3, callback=on_terminate)
		self.cpu = pl.array(cpu);
		self.x = pl.array(x);
		self.ram = pl.array(mem);
		self.z = pl.array(z);
		self.i0 = pl.array(i0);
		self.i1 = pl.array(i1);
		self.i2 = pl.array(i2);
		self.i3 = pl.array(i3);
Пример #14
0
def kill_process_tree(pid, timeout=3):
    """Kill a hierarchy of processes.

    :param pid: pid of the toplevel process
    :type pid: int | psutil.Process
    :return: True if all processes either don't exist or have been killed,
        False if there are some processes still alive.
    :rtype: bool
    """
    import psutil

    if isinstance(pid, psutil.Process):
        parent_process = pid
    else:
        try:
            parent_process = psutil.Process(pid)
        except psutil.NoSuchProcess as err:
            e3.log.debug(err)
            return True

    logger.debug('kill_process_tree %s', parent_process)

    def kill_process(proc):
        """Kill a process, catching all psutil exceptions.

        :param proc: process to kill
        :type proc: psutil.Process
        """
        try:
            logger.debug('kill_process_tree %s', pid)
            proc.kill()
            logging.info('%s process killed pid:%s (%s)',
                         'parent' if proc.pid == pid else 'child',
                         proc.pid,
                         proc.cmdline())
        except psutil.Error as err:
            e3.log.debug(err)
            pass

    try:
        children = parent_process.children(recursive=True)
    except psutil.NoSuchProcess as err:
        e3.log.debug(err)
        return True

    # Kill the parent first to not let him spawn new child processes
    kill_process(parent_process)

    for child_process in children:
        kill_process(child_process)

    try:
        psutil.wait_procs(children, timeout=timeout)
        parent_process.wait(timeout=timeout)
        return True
    except psutil.TimeoutExpired as err:
        e3.log.debug(err)
        return False
Пример #15
0
def kill_proc_tree(pid, including_parent=True):    
    parent = psutil.Process(pid)
    children = parent.children(recursive=True)
    for child in children:
        child.kill()
    psutil.wait_procs(children, timeout=5)
    if including_parent:
        parent.kill()
        parent.wait(5)
Пример #16
0
    def _terminate_win(self, pid):
        # On Windows we spawned a shell on Popen, so we need to
        # terminate all child processes as well
        import psutil

        parent = psutil.Process(pid)
        children = parent.children(recursive=True)
        for child in children:
            child.kill()
        psutil.wait_procs(children, timeout=5)
Пример #17
0
def kill_process_tree(pid):
	try:
		main_proc = psutil.Process(pid)
	except (psutil.NoSuchProcess, psutil.AccessDenied) as ex:
		return
	processes = main_proc.children(recursive=True)
	processes.append(main_proc)
	for proc in processes:
		proc.terminate()
	psutil.wait_procs(processes)
Пример #18
0
    def stop(self):
        """Stop the daemon."""
        if self.pidfile is None:
            raise DaemonError('Cannot stop daemon without PID file')

        pid = self._read_pidfile()
        if pid is None:
            # I don't think this should be a fatal error
            self._emit_warning('{prog} is not running'.format(prog=self.prog))
            return

        self._emit_message('Stopping {prog} ... '.format(prog=self.prog))

        pgid = os.getpgid(pid)
        for gproc in psutil.process_iter():
            try:
                if os.getpgid(gproc.pid) == pgid and gproc.pid != 0:
                    try:
                        # Try to terminate the process
                        os.kill(gproc.pid, signal.SIGTERM)
                    except OSError as ex:
                        self._emit_failed()
                        self._emit_error(str(ex))
                        sys.exit(1)
            
                    _, alive = psutil.wait_procs([psutil.Process(gproc.pid)], timeout=self.stop_timeout)
                    if alive:
                        # The process didn't terminate for some reason
                        if self.kill_timeout:
                            try:
                                os.kill(gproc.pid, signal.SIGKILL)
                            except OSError as ex:
                                self._emit_failed()
                                self._emit_error(str(ex))
                                sys.exit(1)
                    
                            _, alive = psutil.wait_procs([psutil.Process(gproc.pid)], timeout=self.kill_timeout)
                            if alive:
                                self._emit_failed()
                                self._emit_error('Timed out while waiting for process (PID {pid}) '
                                                 'to terminate'.format(pid=gproc.pid))
                                sys.exit(1)
                            else:
                                self._emit_message('{pid} '.format(pid=gproc.pid))
                        else:
                            self._emit_failed()
                            self._emit_error('Timed out while waiting for process (PID {pid}) '
                                             'to terminate'.format(pid=gproc.pid))
                            sys.exit(1)
                    else:
                        self._emit_message('{pid} '.format(pid=gproc.pid))

            except psutil.Error:
                pass
        self._emit_ok()
Пример #19
0
def kill_descendant_processes(logger, pids_to_kill=None):
    """
    Kills all descendant processes of this process.

    :param logger: logger
    :type logger: logging.Logger
    :param pids_to_kill: if specified, kill only these PIDs
    :type pids_to_kill: list[int]
    """
    # First try SIGTERM
    this_process = psutil.Process(os.getpid())

    # Only check child processes to ensure that we don't have a case
    # where a child process died but the PID got reused.
    descendant_processes = [x for x in this_process.children(recursive=True)
                            if x.is_running()]
    if pids_to_kill:
        descendant_processes = [x for x in descendant_processes
                                if x.pid in pids_to_kill]

    if len(descendant_processes) == 0:
        logger.debug("There are no descendant processes that can be killed")
        return
    logger.warn("Terminating descendant processes of {} PID: {}"
                .format(this_process.cmdline(),
                        this_process.pid))
    for descendant in descendant_processes:
        logger.warn("Terminating descendant process {} PID: {}"
                    .format(descendant.cmdline(), descendant.pid))
        descendant.terminate()
    logger.warn("Waiting up to {}s for processes to exit..."
                .format(TIME_TO_WAIT_AFTER_SIGTERM))
    try:
        psutil.wait_procs(descendant_processes, TIME_TO_WAIT_AFTER_SIGTERM)
        logger.warn("Done waiting")
    except psutil.TimeoutExpired:
        logger.warn("Ran out of time while waiting for "
                    "processes to exit")
    # Then SIGKILL
    descendant_processes = [x for x in this_process.children(recursive=True)
                            if x.is_running()]
    if pids_to_kill:
        descendant_processes = [x for x in descendant_processes
                                if x.pid in pids_to_kill]

    if len(descendant_processes) > 0:
        for descendant in descendant_processes:
            logger.warn("Killing descendant process {} PID: {}"
                        .format(descendant.cmdline(), descendant.pid))
            descendant.kill()
            descendant.wait()
        logger.warn("Killed all descendant processes of {} PID: {}"
                    .format(this_process.cmdline(),
                            this_process.pid))
Пример #20
0
 def CloseVenusAdaptor(self,ProcessBat):
     
     """Close Venus Adaptor""" 
     self.Log.info(u'========Close Venus Adaptor========') 
     ParentProcess = psutil.Process(ProcessBat.pid)
     ChildrenProcess = ParentProcess.children(recursive=True)
     for ChildProcess in ChildrenProcess:
         self.Log.info(u'Close child process') 
         ChildProcess.kill()
     psutil.wait_procs(ChildrenProcess, timeout=5)
     ParentProcess.kill()
     ParentProcess.wait(10)    
Пример #21
0
def restart_opentxs_notary():
    '''opentxs-notary must be on the PATH'''
    # kill existing processes
    for proc in psutil.process_iter():
        if proc.name() == "opentxs-notary":
            proc.kill()
            psutil.wait_procs([proc], timeout=10)

    create_fresh_ot_config()

    # start new
    os.system("opentxs-notary > opentxs-notary.log 2>&1 &")
Пример #22
0
def closeTool(toolName):
    for proc in psutil.process_iter():
        if "python.exe" in proc.name():
            process_toolName = os.path.splitext(os.path.basename(proc.cmdline()[-1:][0]))[0]
            if process_toolName == toolName:

                children = proc.children(recursive=True)
                for child in children:
                    child.kill()
                psutil.wait_procs(children, timeout=5)

                proc.kill()

    return True
Пример #23
0
    def _terminate(self):
        """Terminate our process"""
        if os.name == 'nt':
            # On Windows we spawned a shell on Popen, so we need to
            # terminate all child processes as well
            import psutil

            parent = psutil.Process(self.proxy_process.pid)
            children = parent.children(recursive=True)
            for child in children:
                child.kill()
            psutil.wait_procs(children, timeout=5)
        else:
            self.proxy_process.terminate()
Пример #24
0
def killProcTree(pid, including_parent=True):
   """Kill a parent process with its children"""
   parent = psutil.Process(pid)
   children = parent.children(recursive=True)
   for child in children:
      child.kill()
      psutil.wait_procs(children, timeout=5)
   if including_parent:
      try:
         parent.kill()
         parent.wait(20)
         print 'Process killed'
      except:
         print 'Error : process still alive'
         raise
Пример #25
0
def kill_process_tree(parent_pid, recursive=False):
    proc = psutil.Process(parent_pid)
    children = proc.children(recursive)

    # Send a SIGTERM (Ctrl-C) to the main process
    proc.terminate()

    # If children processes don't stop gracefully in time,
    # slaughter them by force.
    _, still_alive = psutil.wait_procs(children, timeout=5)
    for p in still_alive:
        p.kill()

    # Wait until this process is running.
    n = 0
    timeout = 10
    while proc.is_running():
        if n > timeout:
            LOG.warning("Waiting for process %s to stop has been timed out"
                        "(timeout = %s)! Process is still running!",
                        parent_pid, timeout)
            break

        time.sleep(1)
        n += 1
Пример #26
0
def fork_content_save(cache_name, contents, presaver, saver, cleaner, timeout, seen_pids):
    children = _exclude_zombie_procs([proc for proc in psutil.Process().children(recursive=False)
            if proc.pid in seen_pids[cache_name]])
    cache_pids = set(child.pid for child in children)
    terminated_pids = seen_pids[cache_name] - cache_pids
    for pid in terminated_pids:
        # Slay the undead... they mingle with the living...
        try: os.waitpid(pid, 0)
        except OSError: pass
        if cleaner:
            cleaner(cache_name, _tmp_pid_extensions(pid))
    seen_pids[cache_name] = cache_pids

    exts = _tmp_pid_extensions()
    try:
        fork_pid = os.fork()
    except OSError as e:
        print(("Warning, saving {} synchronously: {} ".format(cache_name, repr(e)) +
            "-- you're out of memory or you might be out of shared memory (check kernel.shmmax)"))
        if presaver:
            presaver(cache_name, contents, exts)
        saver(cache_name, contents, exts)
        return
    except AttributeError:
        # Windows has no fork... TODO make windows async saver
        if presaver:
            presaver(cache_name, contents, exts)
        saver(cache_name, contents, exts)
        return

    if fork_pid != 0:
        cache_pids.add(fork_pid)
    else:
        try:
            pid = os.getpid()
            pid_exts = _tmp_pid_extensions(pid)
        except Exception as e:
            print("Warning: ignored error in '{}' cache saver - {}".format(cache_name, repr(e)))
        try:
            if presaver:
                presaver(cache_name, contents, pid_exts)

            # Refilter our zombies
            children = _exclude_zombie_procs(children)
            if children:
                gone, alive_and_undead = psutil.wait_procs(children, timeout=timeout)
                # Avoid killing processes that have since died
                alive = _exclude_zombie_procs(alive_and_undead)
                for p in alive:
                    print("Warning killing previous save for '{}' cache on pid {}".format(cache_name, p.pid))
                    p.kill()
            saver(cache_name, contents, pid_exts)
        except Exception as e:
            if cleaner:
                try: cleaner(cache_name, contents, pid_exts)
                except: pass
            print("Warning: ignored error in '{}' cache saver - {}".format(cache_name, repr(e)))
        finally:
            # Exit aggresively -- we don't want cleanup to occur
            os._exit(0)
Пример #27
0
  def restartInstance(self, pid, instanceName, enabled):
    """Kill a process which is then restarted automatically."""
    if not (self.enabled and enabled):
      self.log.info("Restarting is disabled, please restart %s manually" % instanceName)
      self.accounting[instanceName]["Treatment"] = "Please restart it manually"
      return S_OK(NO_RESTART)

    try:
      agentProc = psutil.Process(int(pid))
      processesToTerminate = agentProc.children(recursive=True)
      processesToTerminate.append(agentProc)

      for proc in processesToTerminate:
        proc.terminate()

      _gone, alive = psutil.wait_procs(processesToTerminate, timeout=5,
                                       callback=partial(self.on_terminate, instanceName))
      for proc in alive:
        self.log.info("Forcefully killing process %s" % proc.pid)
        proc.kill()

      return S_OK()

    except psutil.Error as err:
      self.logError("Exception occurred in terminating processes", "%s" % err)
      return S_ERROR()
Пример #28
0
    def stop(self):
        """Stop the daemon."""
        if self.pidfile is None:
            raise DaemonError('Cannot stop daemon without PID file')

        pid = self._read_pidfile()
        if pid is None:
            # I don't think this should not be a fatal error
            self._emit_warning('{prog} is not running'.format(prog=self.prog))
            return

        self._emit_message('Stopping {prog} ... '.format(prog=self.prog))

        try:
            # Try to terminate the process
            os.kill(pid, signal.SIGTERM)
        except OSError as ex:
            self._emit_failed()
            self._emit_error(str(ex))
            sys.exit(1)

        _, alive = psutil.wait_procs([psutil.Process(pid)], timeout=self.stop_timeout)
        if alive:
            # The process didn't terminate for some reason
            self._emit_failed()
            self._emit_error('Timed out while waiting for process (PID {pid}) '
                             'to terminate'.format(pid=pid))
            sys.exit(1)

        self._emit_ok()
Пример #29
0
    def wait(self, cpus):
        while len(self._running):
            if windows:
                waitlist = [p for p, j in self._running]
                gone, alive = wait_procs(waitlist, timeout=1)
                for g in gone:
                    for p, j in self._running:
                        if p == g:
                            self._running.remove((p, j))
                            if g.returncode:
                                self.handle_error(g.returncode, j)
                                j['status'] = 'X'
                            else:
                                j['status'] = 'F'
                            self._cpus_free += j['cpu']
                            break
            else:
                w = os.waitpid(-1, 0)
                for p, j in self._running:
                    if p.pid == w[0]:
                        self._running.remove((p, j))
                        exitcode = os.WEXITSTATUS(w[1])
                        if exitcode:
                            self.handle_error(exitcode, j)
                            j['status'] = 'X'
                        else:
                            j['status'] = 'F'
                        self._cpus_free += j['cpu']
                        break

            if cpus and self._cpus_free >= cpus:
                return
        return
Пример #30
0
def kill_proc_tree(pid, including_parent=True, timeout=5):
    try:
        parent = Process(pid)
    except NoSuchProcess:
        return
    children = parent.children(recursive=True)
    for child in children:
        if verbose.kill:
            print("killing {}".format(child.pid))
        try:
            child.kill()
            child.terminate()
        except NoSuchProcess:
            pass
    gone, still_alive = wait_procs(children, timeout=timeout)
    if including_parent:
        try:
            if verbose.kill:
                print("killing {}".format(parent.pid))
            parent.kill()
            parent.terminate()
            try:
                parent.wait(timeout)
            except TimeoutExpired:
                print("timeout expired, process may still be around: {}".format(parent.pid))
        except NoSuchProcess:
            pass
Пример #31
0
def kill_proc_tree(
    pid,
    sig=signal.SIGTERM,
    include_parent=True,
    timeout=None,
    on_terminate=None,
):
    """Kill a process tree (including grandchildren) with signal
    "sig" and return a (gone, still_alive) tuple.
    "on_terminate", if specified, is a callaback function which is
    called as soon as a child terminates.
    """
    if pid == os.getpid():
        raise RuntimeError("I refuse to kill myself")
    try:
        parent = psutil.Process(pid)
        children = parent.children(recursive=True)
        if include_parent:
            children.append(parent)
        for p in children:
            p.send_signal(sig)
        gone, alive = psutil.wait_procs(children,
                                        timeout=timeout,
                                        callback=on_terminate)
        if alive:
            # send SIGKILL
            for p in alive:
                logger.warn("process {} survived SIGTERM; trying SIGKILL" % p)
                p.kill()
            gone, alive = psutil.wait_procs(alive,
                                            timeout=timeout,
                                            callback=on_terminate)
            if alive:
                # give up
                for p in alive:
                    logger.error("process {} survived SIGKILL; giving up" % p)
        return (gone, alive)
    except psutil.NoSuchProcess as e:
        return (None, None)
Пример #32
0
    def terminate(self):
        """ Terminate the subprocess """
        if self.command == "train":
            print("Sending Exit Signal", flush=True)
            try:
                now = time()
                if os.name == "nt":
                    os.kill(self.process.pid, signal.CTRL_BREAK_EVENT)  # pylint: disable=no-member
                else:
                    self.process.send_signal(signal.SIGINT)
                while True:
                    timeelapsed = time() - now
                    if self.process.poll() is not None:
                        break
                    if timeelapsed > 30:
                        raise ValueError("Timeout reached sending Exit Signal")
                return
            except ValueError as err:
                print(err)
        else:
            print("Terminating Process...")
            children = psutil.Process().children(recursive=True)
            for child in children:
                child.terminate()
            _, alive = psutil.wait_procs(children, timeout=10)
            if not alive:
                print("Terminated")
                return

            print("Termination timed out. Killing Process...")
            for child in alive:
                child.kill()
            _, alive = psutil.wait_procs(alive, timeout=10)
            if not alive:
                print("Killed")
            else:
                for child in alive:
                    print("Process {} survived SIGKILL. "
                          "Giving up".format(child))
Пример #33
0
def kill_process_tree(pid, force=False, timeout=None):
    import psutil
    import signal

    if force:
        sig = signal.SIGKILL
    else:
        sig = signal.SIGTERM
    root = psutil.Process(pid)
    procs = [root] + root.children(recursive=True)
    for proc in procs:
        proc.send_signal(sig)
    return psutil.wait_procs(procs, timeout=timeout)
Пример #34
0
def check_tracked_pids():
    while (True):
        gone, alive = psutil.wait_procs(pids_tracked.keys(), timeout=0.1)
        for proc in gone:
            total_time = int(time.time() - pids_tracked[proc][1])
            slack_client.api_call(
                "chat.postMessage",
                channel=pids_tracked[proc][0],
                text="Process `{}` with PID `{}` finished after `{}`.".format(
                    pids_tracked[proc][2], proc.pid,
                    str(datetime.timedelta(seconds=total_time))))
            pids_tracked.pop(proc)
        time.sleep(1)
Пример #35
0
 def teardown(self):
     procs = []
     for proc in psutil.process_iter():
         try:
             if (proc.exe() == sys.executable
                     and self.realpath in proc.cmdline()):
                 proc.terminate()
                 procs.append(proc)
         except (psutil.NoSuchProcess, psutil.AccessDenied, OSError):
             continue
     if psutil.wait_procs(procs, timeout=1)[1]:
         raise OSError('Failed to terminate subprocesses')
     shutil.rmtree(self.dirname)
Пример #36
0
def stop_gui_processes():
    import psutil
    active_pids = {p.pid for p in multiprocessing.active_children()}
    kill_procs = [pr for pr in _launch_qt_processes if pr.pid in active_pids]
    for p in kill_procs:
        try:
            # active_children apparently contains false positives sometimes
            p.terminate()
        except psutil.NoSuchProcess:
            pass
    gone, still_alive = psutil.wait_procs(kill_procs, timeout=1)
    for p in still_alive:
        p.kill()
Пример #37
0
 def _process_stop(self):
     """Stop any instances of a running binary"""
     if self.stop_cmd is not None:
         return self._run_command(self.stop_cmd)
     else:
         procs = self._find_application_processes()
         for p in procs:
             p.terminate()
         gone, alive = psutil.wait_procs(procs, 10)
         for p in alive:
             p.kill()
         self._log.info('Stopped pid(s) {0}'.format([p.pid for p in procs]))
         return 0
Пример #38
0
    def end(self):
        """
        Terminate (and then kill) the manager process launched.
        :return:
        """
        if not self._process:
            self.log.warn('Ending without manager process.')
            return
        this_process = psutil.Process(os.getpid())
        try:
            manager_process = psutil.Process(self._process.pid)
        except psutil.NoSuchProcess:
            self.log.info("Manager process not running.")
            return

        # First try SIGTERM
        if manager_process.is_running() \
                and manager_process.pid in [x.pid for x in this_process.children()]:
            self.log.info("Terminating manager process: {}".format(
                manager_process.pid))
            manager_process.terminate()
            # TODO: Remove magic number
            timeout = 5
            self.log.info(
                "Waiting up to {}s for manager process to exit...".format(
                    timeout))
            try:
                psutil.wait_procs({manager_process}, timeout)
            except psutil.TimeoutExpired:
                self.log.debug("Ran out of time while waiting for "
                               "processes to exit")

        # Then SIGKILL
        if manager_process.is_running() \
                and manager_process.pid in [x.pid for x in this_process.children()]:
            self.log.info("Killing manager process: {}".format(
                manager_process.pid))
            manager_process.kill()
            manager_process.wait()
Пример #39
0
def kill_process_tree(iter_pid, kill_signal):
    """
    Kill a process tree (including grandchildren).

    """
    if psutil is None:
        return
    for pid in iter_pid:

        assert pid != os.getpid(), \
               'A process should not attempt to kill itself'
        try:
            parent = psutil.Process(pid)
        except psutil.NoSuchProcess:
            continue

        children = parent.children(recursive=True)
        children.append(parent)
        for proc in children:
            proc.send_signal(kill_signal)

        psutil.wait_procs(children)
Пример #40
0
 def stop_tor_daemon(self):
     timeout = 50  # in secs
     if self.tor_daemon_proc:
         if platform.system() == "Windows":
             subprocess.run("Taskkill /IM tor.exe /F")
         self.tor_daemon_proc.terminate()
         procs = psutil.Process().children()
         for p in procs:
             p.terminate()
         _, alive = psutil.wait_procs(procs, timeout=timeout)
         for p in alive:
             logger.info("tor daemon did not terminated in time, killing!")
             p.kill()
Пример #41
0
  def shutdown(self):
    if self.proc is None: return

    # graceful shutdown
    try:
      self.execute("""
        const appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1'].getService(Components.interfaces.nsIAppStartup);
        appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit);
      """)
    except:
      pass

    def on_terminate(proc):
        utils.print("process {} terminated with exit code {}".format(proc, proc.returncode))

    zotero = psutil.Process(self.proc.pid)
    alive = zotero.children(recursive=True)
    alive.append(zotero)

    for p in alive:
      try:
        p.terminate()
      except psutil.NoSuchProcess:
        pass
    gone, alive = psutil.wait_procs(alive, timeout=5, callback=on_terminate)

    if alive:
      for p in alive:
        utils.print("process {} survived SIGTERM; trying SIGKILL" % p)
        try:
          p.kill()
        except psutil.NoSuchProcess:
          pass
      gone, alive = psutil.wait_procs(alive, timeout=5, callback=on_terminate)
      if alive:
        for p in alive:
          utils.print("process {} survived SIGKILL; giving up" % p)
    self.proc = None
    assert not running('Zotero')
Пример #42
0
def reap_process_group(pid, log, sig=signal.SIGTERM,
                       timeout=DEFAULT_TIME_TO_WAIT_AFTER_SIGTERM):
    """
    Tries really hard to terminate all children (including grandchildren). Will send
    sig (SIGTERM) to the process group of pid. If any process is alive after timeout
    a SIGKILL will be send.

    :param log: log handler
    :param pid: pid to kill
    :param sig: signal type
    :param timeout: how much time a process has to terminate
    """

    def on_terminate(p):
        log.info("Process %s (%s) terminated with exit code %s", p, p.pid, p.returncode)

    if pid == os.getpid():
        raise RuntimeError("I refuse to kill myself")

    parent = psutil.Process(pid)

    children = parent.children(recursive=True)
    children.append(parent)

    log.info("Sending %s to GPID %s", sig, os.getpgid(pid))
    os.killpg(os.getpgid(pid), sig)

    gone, alive = psutil.wait_procs(children, timeout=timeout, callback=on_terminate)

    if alive:
        for p in alive:
            log.warn("process %s (%s) did not respond to SIGTERM. Trying SIGKILL", p, pid)

        os.killpg(os.getpgid(pid), signal.SIGKILL)

        gone, alive = psutil.wait_procs(alive, timeout=timeout, callback=on_terminate)
        if alive:
            for p in alive:
                log.error("Process %s (%s) could not be killed. Giving up.", p, p.pid)
Пример #43
0
    def terminate(self, *, kill_proc_tree=True, timeout=20):
        """Recursively terminates process tree.

        This is the default behavior unless explicitly disabled by setting
        kill_proc_tree keyword-only parameter to false when calling
        ``XProcessInfo.terminate``.

        :param kill_proc_tree: Enable/disable recursive process tree
                               termination. Defaults to True.
        :param timeout: Maximum time in seconds to wait on process termination.
                        When timeout is reached after sending SIGTERM, this
                        method will attempt to SIGKILL the process and
                        return ``-1`` in case the operation times out again.
        return codes:
            0   no work to do
            1   terminated
            -1  failed to terminate
        """
        if not self.pid:
            return 0
        try:
            parent = psutil.Process(self.pid)
        except psutil.NoSuchProcess:
            return 0
        try:
            kill_list = [parent]
            if kill_proc_tree:
                kill_list += parent.children(recursive=True)
            for p in kill_list:
                p.send_signal(signal.SIGTERM)
            _, alive = psutil.wait_procs(kill_list, timeout=timeout)
            for p in alive:
                p.send_signal(signal.SIGKILL)
            _, alive = psutil.wait_procs(kill_list, timeout=timeout)
            if alive:
                return -1
        except psutil.Error:
            return -1
        return 1
    def terminate_process_tree(self,
                               process,
                               include_parent=True,
                               timeout=None):
        print(colored('process id: {}'.format(process.pid), 'blue'))

        procs = psutil.Process(process.pid).children(recursive=True)

        print(colored(procs, 'red'))

        # send SIGTERM
        for ch_p in procs:
            try:
                print(colored(ch_p, 'red'))
                ch_p.terminate()
            except psutil.NoSuchProcess:
                pass
        gone, alive = psutil.wait_procs(procs, timeout=timeout)
        if alive:
            # send SIGKILL
            for ch_p in alive:
                print("process {} survived SIGTERM; trying SIGKILL" % process)
                try:
                    ch_p.kill()
                except psutil.NoSuchProcess:
                    pass
            gone, alive = psutil.wait_procs(alive, timeout=timeout)
            if alive:
                # give up
                for ch_p in alive:
                    print("process {} survived SIGKILL; giving up" % process)
                    return False

        if include_parent is True:
            process.terminate()
            process.join()
        # self.hang_process_list.remove(process)

        return True
Пример #45
0
def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
                   timeout=None, on_terminate=None):

    assert pid != os.getpid(), "won't kill myself"
    parent = psutil.Process(pid)
    children = parent.children(recursive=True)
    if include_parent:
        children.append(parent)
    for p in children:
        p.send_signal(sig)
    gone, alive = psutil.wait_procs(children, timeout=timeout,
                                    callback=on_terminate)
    return (gone, alive)
Пример #46
0
def kill(pid, timeout=3):
    def on_terminate(proc):
        print('process {} terminated with exit code {}'.format(
            proc, proc.returncode))

    # TERM then KILL
    try:
        proc = psutil.Process(pid)
        proc.terminate()
        _, alive = psutil.wait_procs([proc],
                                     timeout=timeout,
                                     callback=on_terminate)
        if alive:
            proc.kill()
            _, alive = psutil.wait_procs([proc],
                                         timeout=timeout,
                                         callback=on_terminate)
            if alive:
                return False
    except psutil.NoSuchProcess:
        return True
    return True
Пример #47
0
def kill_proc_tree(p, timeout=None, on_terminate=None):
    '''
        p = pid of root process
    '''
    if isinstance(p, int):
        p = psutil.Process(p)
    elif not isinstance(p, psutil.Process):
        p = psutil.Process(p.pid)
    ch = [p] + p.children(recursive=True)
    for c in ch:
        c.kill()
    succ, err = psutil.wait_procs(ch, timeout=timeout, callback=on_terminate)
    return (succ, err)
Пример #48
0
    def kill_process_tree(self, sig=signal.SIGTERM, include_parent=False, timeout=None, on_terminate=None):
        pid = self.pid
        parent = psutil.Process(pid)
        children = parent.children(recursive=True)

        if include_parent:
            children.append(parent)
        for p in children:
            p.send_signal(sig)

        gone, alive = psutil.wait_procs(children, timeout=timeout,
                                        callback=on_terminate)
        return (gone, alive)
Пример #49
0
def kill_proc_tree(p, timeout=None, on_terminate=None):
    if isinstance(p, int):
        p = psutil.Process(p)
    elif not isinstance(p, psutil.Process):
        p = psutil.Process(p.pid)
    ch = [p] + p.children(recursive=True)
    for c in ch:
        try:
            c.kill()
        except psutil.NoSuchProcess:
            continue
    succ, err = psutil.wait_procs(ch, timeout=timeout, callback=on_terminate)
    return (succ, err)
Пример #50
0
 def _close_vlc(self):
     """Kills Vlc process.
     """
     if os.name == 'nt':
         PROCNAME = "vlc.exe"
     else:
         PROCNAME = "vlc"
     for proc in psutil.process_iter():
         if proc.name() == PROCNAME:
             proc.kill()
             gone, alive = psutil.wait_procs([proc], timeout=3)
             return len(alive) == 0
     return True
Пример #51
0
    def end(self):
        """
        Terminate (and then kill) the manager process launched.
        :return:
        """
        if not self._process:
            self.log.warn('Ending without manager process.')
            return
        this_process = psutil.Process(os.getpid())
        try:
            manager_process = psutil.Process(self._process.pid)
        except psutil.NoSuchProcess:
            self.log.info("Manager process not running.")
            return

        # First try SIGTERM
        if manager_process.is_running() \
                and manager_process.pid in [x.pid for x in this_process.children()]:
            self.log.info("Terminating manager process: %s", manager_process.pid)
            manager_process.terminate()
            # TODO: Remove magic number
            timeout = 5
            self.log.info("Waiting up to %ss for manager process to exit...", timeout)
            try:
                psutil.wait_procs({manager_process}, timeout)
            except psutil.TimeoutExpired:
                self.log.debug("Ran out of time while waiting for "
                               "processes to exit")

        # Then SIGKILL
        if manager_process.is_running() \
                and manager_process.pid in [x.pid for x in this_process.children()]:
            self.log.info("Killing manager process: %s", manager_process.pid)
            manager_process.kill()
            manager_process.wait()
        # TODO: bring it back once we replace process termination above with multiprocess-friendly
        #       way (https://issues.apache.org/jira/browse/AIRFLOW-4440)
        # self._result_queue.join()
        self._manager.shutdown()
Пример #52
0
    def stop_proc(self, proc, stop_children=True, timeout=10):
        return_value = False
        with self._lock:
            # if process already dead, say so:
            try:
                returncode = proc.poll()
            except AttributeError:
                returncode = 0
            proc_pid = proc.pid
            try:
                if proc.is_running() is False:
                    logger.info("Trying to stop \"{0}\", but it is not running. Has returncode:"
                                " {1}".format(proc.pid, returncode))
                    if proc.pid in self._processes:
                        self._processes.pop(proc_pid)
                    return True
                child_procs = proc.children(recursive=True)
                proc_cmdline = proc.cmdline()
                proc.terminate()
                (gone, alive) = psutil.wait_procs([proc], timeout)
                if gone:
                    logger.info("Terminated command {}".format(proc_cmdline))
                    if proc in self._processes:
                        self._processes.pop(proc_pid)
                    return_value = True
                for p in alive:
                    logger.info("Process did not terminate, killing '{}'".format(proc.cmdline()))
                    p.kill()
                    if proc.is_running():
                        return_value = False
            except psutil.AccessDenied:
                logger.warning("Could not access process {}".format(proc.pid))
                return return_value
            except psutil.NoSuchProcess:
                logger.info("Process {} no longer exists".format(proc.pid))
                return return_value

            # Stop Child Processes:
            if stop_children:
                for proc in child_procs:
                    logger.warning("Stopping Child Process {}".format(proc.pid))
                    try:
                        self.stop_proc(proc, stop_children=False, timeout=timeout)
                    except psutil.NoSuchProcess:
                        logger.info("Child process {} already stopped".format(proc.pid))
                    except psutil.AccessDenied:
                        logger.warning("Child process {} could not be accessed".format(proc.pid))
                    except Exception as e:
                        logger.exception("Unknown error stopping process")
                        raise e
            return return_value
Пример #53
0
def killjobs(args=None):
    """
    kill all processes which contain a string passed from the command line

    Parameters
    ----------

    args: optional -- argparse argument containing  the string
          to search for in args.snip
          if missing then args will be supplied from the command line
    """
    parser = make_parser()
    args = parser.parse_args(args)
    keepit = {}
    keys = ['time', 'name', 'cmdline', 'proc']
    for proc in psutil.process_iter():
        print(f'interating {proc}')
        try:
            the_dict = dict(
                zip(keys,
                    (proc.create_time(), proc.exe(), proc.cmdline(), proc)))
            keepit[proc.pid] = make_tuple(the_dict)
        except (psutil.ZombieProcess, psutil.AccessDenied,
                psutil.NoSuchProcess):
            print('hit exception')
            pass
        except FileNotFoundError:
            print('file not found')
    print('in killjobs.py, looking for {}'.format(args.snip))
    #
    # don't kill this process or the emacs python parser
    #
    proclist = []
    for the_tup in keepit.values():
        string_cmd = ' '.join(the_tup.cmdline)
        if the_tup.name.find(args.snip) > -1 and \
           string_cmd.find('killjobs') == -1 and \
           string_cmd.find('elpy') == -1:
            proclist.append(the_tup)

    proconly = [item.proc for item in proclist]
    print(f'ready to kill {proconly}')
    for item in proconly:
        cmd_string = ' '.join(item.cmdline())
        print('terminating: {}'.format(cmd_string))
    [proc.terminate() for proc in proconly]

    gone, alive = psutil.wait_procs(proconly, timeout=3, callback=on_terminate)

    for p in alive:
        p.kill()
Пример #54
0
def run_on_cores_restart(process_info_list,
                         copies=1,
                         cores=None,
                         rstrt_even=False):
    """ Take the output from parse_file and launch the processes on cores=[cpu,...] """
    # Ensure size of cores and process_info_list is same
    num_procs = len(process_info_list)
    if num_procs > 2:
        print("More than 2 processes")
        exit(1)
    # one more check for len(process_info_list) == len(cores)
    if cores is None:
        cores = range(copies)

    restarted = []
    p_list = []
    proc_dict = {}
    for cpu in cores:
        process_info = process_info_list[cpu % num_procs]
        p = launch_on_core(process_info, cpu)
        p_dict_loc = p.as_dict()
        p_dict_loc['work_info'] = process_info_list[cpu % num_procs]
        proc_dict[p_dict_loc['pid']] = p_dict_loc
        p_list.append(p)

    def print_time(proc):
        """ Print Process Info on compeletion """
        end_time = time()
        p_dic = proc_dict[proc.pid]
        print(p_dic['name'], p_dic['pid'], p_dic['cpu_num'],
              str(end_time - p_dic['create_time']))
        _p_rst = None
        if rstrt_even and p_dic['cpu_num'] % 2 == 0:
            _p_rst = Process(target=run_on_core_forever,
                             args=(process_info_list[p_dic['cpu_num'] %
                                                     num_procs],
                                   p_dic['cpu_num']))
            _p_rst.start()
            restarted.append(_p_rst)

    gone, alive = psutil.wait_procs(p_list, timeout=None, callback=print_time)
    for _p in alive:
        _p.kill()
    if len(restarted) >= 1:
        # kill all restrted processes
        for _proc in restarted:
            try:
                _proc.terminate()
            except:
                pass
    return
Пример #55
0
def terminate_process(proc, proc_name):
  """Terminates the process.

  If an error occurs ignore it, just print out a message.

  Args:
    proc: A subprocess to terminate.
    proc_name: A name of process.
  """
  try:
    LOGGER.info('Killing hung process %s' % proc.pid)
    proc.terminate()
    attempts_to_kill = 3
    ps = psutil.Process(proc.pid)
    for _ in range(attempts_to_kill):
      # Check whether proc.pid process is still alive.
      if ps.is_running():
        LOGGER.info(
            'Process %s is still alive! %s process might block it.',
            psutil.Process(proc.pid).name(), proc_name)
        running_processes = [
            p for p in psutil.process_iter()
            # Use as_dict() to avoid API changes across versions of psutil.
            if proc_name == p.as_dict(attrs=['name'])['name']]
        if not running_processes:
          LOGGER.debug('There are no running %s processes.', proc_name)
          break
        LOGGER.debug('List of running %s processes: %s'
                     % (proc_name, running_processes))
        # Killing running processes with proc_name
        for p in running_processes:
          p.send_signal(signal.SIGKILL)
        psutil.wait_procs(running_processes)
      else:
        LOGGER.info('Process was killed!')
        break
  except OSError as ex:
    LOGGER.info('Error while killing a process: %s' % ex)
Пример #56
0
def reap_process_tree(pid, wait_timeout=settings.ACTIVITY_SIGTERM_WAIT_SEC):
    """
    TERMinates (and KILLs) if necessary a process and its descendants.

    See also: https://psutil.readthedocs.io/en/latest/#kill-process-tree.

    :param pid: Process ID
    :type pid: int
    :param wait_timeout: Wait timeout
    :type wait_timeout: float
    """

    def on_terminate(p):
        logger.info('process: terminated pid={} retcode={}'.format(p.pid, p.returncode))

    if pid == os.getpid():
        raise RuntimeError('process: cannot terminate self!')
    parent = psutil.Process(pid)
    procs = parent.children(recursive=True)
    procs.append(parent)
    # Terminate
    for p in procs:
        try:
            p.terminate()
        except psutil.NoSuchProcess:
            pass
    _, alive = psutil.wait_procs(procs, timeout=wait_timeout, callback=on_terminate)
    # Kill
    for p in alive:
        logger.warning('process: pid={} status={} did not respond to SIGTERM. Trying SIGKILL'.format(p.pid, p.status()))
        try:
            p.kill()
        except psutil.NoSuchProcess:
            pass
    # Check
    _, alive = psutil.wait_procs(alive)
    for p in alive:
        logger.error('process: pid={} status={} still alive. Giving up!'.format(p.pid, p.status()))
Пример #57
0
def kill_child_processes(parent_pid=None,
                         parent=None,
                         timeout=3,
                         sig=signal.SIGTERM,
                         include_parent=True):
    global log_f
    #current_time = getlocaltime()
    if not parent and not parent_pid:
        return (None, None)
    try:
        if not parent and parent_pid:
            parent = psutil.Process(parent_pid)
    except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied):
        return (None, None)
    if parent.pid == os.getpid():
        include_parent = False
    children = parent.children(recursive=True)
    if include_parent:
        children.append(parent)
    for process in children:
        #msg = '%s\tKilling child process [%d] of [%d]...\n' % (current_time, process.pid, parent.pid)
        #if log_f:
        #log_f.write(msg)
        try:
            process.send_signal(sig)
        except (psutil.NoSuchProcess, psutil.ZombieProcess,
                psutil.AccessDenied):
            pass
    gone, alive = psutil.wait_procs(children, timeout=timeout, callback=None)
    if alive:
        for process in alive:
            try:
                process.kill()  # SEND SIGKILL
            except (psutil.NoSuchProcess, psutil.ZombieProcess,
                    psutil.AccessDenied):
                pass
        gone, alive = psutil.wait_procs(alive, timeout=timeout, callback=None)
    return (gone, alive)
Пример #58
0
    def __wait_procs(self, procs, timeout):
        before = time.time()
        after = before

        alive = procs

        # (old versions of psutil have a bug and return too soon)
        while alive and (after - before) < timeout:
            next_timeout = math.ceil(timeout - (after - before))
            gone, alive = psutil.wait_procs(alive, timeout=next_timeout)
            after = time.time()
            if after < before:
                after = before
        return alive
Пример #59
0
    def _terminate(pid: int,
                   retry_delay: int = 30,
                   start_mode: int = 0) -> None:
        """Terminate the process. Each mode (retry pass) is more aggressive.
        At the end of each attempt if there are active processes mode is
        incremented and another pass is performed.

        Mode:
        - 0: uses process.terminate() on the parent process only.
        - 1: uses process.terminate() on all processes.
        - 2: uses process.kill() on all processes.

        Args:
            retry_delay: Time in seconds to wait before next attempt.
            start_mode: Initial mode.

        Returns:
            None
        """
        LOG.debug(
            "_terminate(%d, retry_delay=%0.2f, start_mode=%d)",
            pid,
            retry_delay,
            start_mode,
        )
        procs = get_processes(pid)
        for mode in range(start_mode, 3):
            LOG.debug("%d running process(es)", len(procs))
            # iterate over and terminate/kill processes
            for proc in procs:
                try:
                    proc.kill() if mode > 1 else proc.terminate()
                except (AccessDenied, NoSuchProcess):  # pragma: no cover
                    pass
                if mode == 0:
                    # only target the parent process on the first pass
                    break
            procs = wait_procs(procs, timeout=retry_delay)[1]
            if not procs:
                LOG.debug("_terminate() was successful")
                break
            LOG.debug("timed out (%0.2f), mode %d", retry_delay, mode)
        else:
            for proc in procs:
                try:
                    LOG.warning("Failed to terminate process %d (%s)",
                                proc.pid, proc.name())
                except (AccessDenied, NoSuchProcess):  # pragma: no cover
                    pass
            raise TerminateError("Failed to terminate browser")
Пример #60
0
def kill_process_list(processes, sig, time_to_die, logger):
    def on_kill(proc):
        logger.info("process %s is killed, exit code %s", proc.pid, proc.returncode)

    for p in processes:
        kill_process(p, sig, logger)

    try:
        gone, alive = psutil.wait_procs(processes, timeout=time_to_die, callback=on_kill)
    except psutil.Error as e:
        logger.error("error to wait the processes to terminate.")
        logger.exception(e)
        alive = processes
    return alive