示例#1
0
文件: util.py 项目: alon/emolog
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
示例#2
0
def run():
	args = parse_the_args()

	if args.run_setup:
		log.debug('Running FSM Setup')
		fsm_setup()
	elif args.start_in_background:
		if os.name == 'NT':
			python = 'python'
		else:
			python = 'python3'

		commands = [python, 'start.py']
		if len(sys.argv) > 1:
			commands.extend(sys.argv[1:])

		process = Popen(commands, stdout=DEVNULL, stderr=DEVNULL)

		with open('fsm.pid', 'w+') as pid_file:
			pid_file.write(str(process.pid))
	elif args.stop:
		with open('fsm.pid') as pid_file:
			pid = int(pid_file.read())

		p = Process(pid)
		p.terminate()
	else:
		main(args)
示例#3
0
def _kill(process: psutil.Process, timeout=5) -> bool:
    """Attempts to kill the process in a fail-safe manner and returns status

    Tries to terminate the process first and waits for it for the given timeout.
    If termination fails - attempts to kill the process instead and wait for it to die.

    Args:
        process(Process):   the process that is to be killed
        timeout(int):       the timeout to wait after each terminate/kill attempt

    Returns:
        obj(bool):          True if the process has been killed/terminated,
                            False if the process is still running after this method returns
    """

    if not process.is_running():
        return True

    try:
        process.terminate()
        try:
            process.wait(timeout)
            return True
        except:
            process.kill()
            try:
                process.wait(timeout)
                return True
            except:
                pass
    except:
        pass

    return not process.is_running()
示例#4
0
 def Terminate(self):
     """Terminate the process"""
     info('ProcessManager::Terminate Name:' + self.Name + ' PID:' +
          str(self.Pid))
     try:
         process = Process(self.Pid)
         process.terminate()
     except Exception as ex:
         error('ProcessInfo::Terminate failed Name:' + self.Name + ' PID:' +
               str(self.Pid) + ' Exception:' + str(ex))
         return False
     return True
示例#5
0
def stop_diamond(conf_path):
    config_file = os.path.join(conf_path, CONFIG_NAME)
    pid = get_pid(config_file)
    if pid:
        diamond_process = Process(pid)
        diamond_process.terminate()
        diamond_process.wait(timeout=DEFAULT_TIMEOUT)

        if diamond_process.is_running():
            raise exceptions.NonRecoverableError("Diamond couldn't be killed")
    else:
        raise exceptions.NonRecoverableError('Failed reading diamond pid file')
    def safe_stop(proc: psutil.Process, kill: bool = False) -> None:
        """Do not crash on already stopped process.

        :param proc: target process
        :type proc: psutil.Process
        :param kill: use SIGKILL instead of SIGTERM
        :type kill: bool
        """
        with contextlib.suppress(psutil.NoSuchProcess):
            if kill:
                proc.kill()
            proc.terminate()
示例#7
0
 async def _close_process(self, proc: psutil.Process):
     try:
         proc.terminate()
         await asyncio.sleep(1)
         try:
             if proc.status() != psutil.STATUS_ZOMBIE:
                 proc.kill()
         except Exception:
             pass
     except Exception:
         pass
     try:
         proc.wait(timeout=0)
     except Exception:
         pass
def get_as_item(p: Process, *extra_actions):
    """Return an item - ready to be appended to the items list and be rendered by Albert.

    if Process is not a valid object (.name or .cmdline raise an exception) then return None
    """
    name_field = cmdline(p)

    if not name_field:
        return None

    try:

        actions = [
            v0.FuncAction("Terminate", lambda: p.terminate()),
            v0.FuncAction("Kill", lambda: p.kill()),
            v0.ClipAction("Get PID", f"{p.pid}"),
            v0.FuncAction(
                "Terminate matching names",
                lambda name=p.name(): kill_by_name(name, signal=signal.SIGTERM
                                                   ),
            ),
            v0.FuncAction("Kill matching names",
                          lambda name=p.name(): kill_by_name(name)),
        ]
        actions = [*extra_actions, *actions]
        return v0.Item(
            id=__title__,
            icon=icon_path,
            text=name_field,
            subtext="",
            completion=p.name(),
            actions=actions,
        )
    except psutil.NoSuchProcess:
        return None
示例#9
0
def terminateOldListener():
    # Assume the listener hasn't been terminated
    terminated = False
    # Check the PID in the run.txt file to see if it's an active python process and if so shut it down
    with open("Data/run.txt", 'r') as check:
        strpid = check.readline()
        if strpid != "":
            try:
                process = Process(int(strpid))
                if process.name() == "python.exe" or process.name() == "pythonw.exe":
                    process.terminate()
                    terminated = True

            except NoSuchProcess:
                pass

    return terminated
示例#10
0
    def _terminate_server(self):
        """."""
        last_print = None
        while True:
            if self._num_connections > 0 and\
                    (last_print is None or
                     last_print != self._num_connections):
                last_print = self._num_connections
                print('Waiting to close {0} pending connections.'.format(
                    self._num_connections))

            if self._num_connections == 0 and\
                    self._list_connection.empty():
                print('Terminating server.')
                pid = getpid()
                proc = Process(pid)
                proc.terminate()
                break
示例#11
0
def killProcs(PID):
    #try:
    p = Process(PID)
    #try:
    while len(p.children()) > 0:
        try:
            child = p.children()[0]
            while len(child.children()) > 0:
                print len(child.children()), child
                child = child.children()[0]
            print "kill"
            child.terminate()
            print "Restart"
        except:
            pass
    try:
        p.terminate()
    except:
        pass
def stop_diamond(conf_path):
    config_file = os.path.join(conf_path, CONFIG_NAME)
    pid = get_pid(config_file)
    if pid:
        need_kill = True
        try:
            diamond_process = Process(pid)
            diamond_process.terminate()
            diamond_process.wait(timeout=DEFAULT_TIMEOUT)
            need_kill = diamond_process.is_running()
        except Error:
            pass
        if need_kill:
            call(["sudo", "kill", str(pid)])
            # diamond deletes the pid file, even if killed
            for _ in range(DEFAULT_TIMEOUT):
                pid = get_pid(config_file)
                if not pid:
                    return
                sleep(1)
    else:
        raise exceptions.NonRecoverableError('Failed reading diamond pid file')
示例#13
0
def stop_diamond(conf_path):
    config_file = os.path.join(conf_path, CONFIG_NAME)
    pid = get_pid(config_file)
    if pid:
        need_kill = True
        try:
            diamond_process = Process(pid)
            diamond_process.terminate()
            diamond_process.wait(timeout=DEFAULT_TIMEOUT)
            need_kill = diamond_process.is_running()
        except Error:
            pass
        if need_kill:
            call(["sudo", "kill", str(pid)])
            # diamond deletes the pid file, even if killed
            for _ in range(DEFAULT_TIMEOUT):
                pid = get_pid(config_file)
                if not pid:
                    return
                sleep(1)
    else:
        raise exceptions.NonRecoverableError('Failed reading diamond pid file')
    def stop(self):
        """
        Stop the Elasticsearch server.

        :rtype : ElasticsearchRunner
        :return: The instance called on.
        """
        if self.is_running():
            server_proc = Process(self.es_state.server_pid)
            server_proc.terminate()
            server_proc.wait()

            if process_exists(self.es_state.server_pid):
                logging.warn('Failed to stop Elasticsearch server process PID %d ...' % self.es_state.server_pid)

            # delete transient directories
            if 'path' in self.es_config:
                if 'log' in self.es_config['path']:
                    log_path = self.es_config['path']['log']
                    logging.info('Removing transient log path %s ...' % log_path)
                    rmtree(log_path)

                if 'data' in self.es_config['path']:
                    data_path = self.es_config['path']['data']
                    logging.info('Removing transient data path %s ...' % data_path)
                    rmtree(data_path)

            # delete temporary config file
            if os.path.exists(self.es_state.config_fn):
                logging.info('Removing transient configuration file %s ...' % self.es_state.config_fn)
                os.remove(self.es_state.config_fn)

            self.es_state = None
            self.es_config = None
        else:
            logging.warn('Elasticsearch is not running ...')

        return self
示例#15
0
def kill_process(max_retry,
                 p_instance: psutil.Process,
                 p_name,
                 kill_process_retry_interval: int = 60):
    if not p_instance:
        return
    count: int = 0
    while psutil.Process(p_instance.pid).status() != psutil.STATUS_ZOMBIE:
        p_instance.terminate()
        time.sleep(kill_process_retry_interval)
        if count % kill_process_retry_interval == 0:
            print(f'terminating process: {p_name}')
        count += 1
        if count > max_retry:
            print(
                f'reach max limitation of retries and failed to terminate process: {p_name}'
            )
            return

    # clear the zombie
    p_instance.kill()
    p_instance = None
    print(f'{p_name} is terminated')
示例#16
0
def stop(**kwargs):
    lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid"))
    if try_systemd_stop():
        logger.info("Stopping rammon daemon with systemd...")
    if lock.is_locked():
        try:
            proc = Process(lock.read_pid())
            proc.terminate()
            try:
                proc.wait(1)
            except TimeoutExpired:
                print("Rammon did not stop gracefully, killing it...")
                proc.kill()
                lock.break_lock()
            logger.info("Rammon stopped successfully")
        except NoSuchProcess:
            logger.warning(
                "Rammon was already stopped, but had not stopped cleanly.\n"
                "Breaking pidfile lock...")
            lock.break_lock()
    else:
        logger.error("Rammon is already stopped")
        sys.exit(1)
 def terminate(process: psutil.Process) -> None:
     process.terminate()
示例#18
0
    https://stackoverflow.com/questions/47818822/can-i-use-tensorboard-with-google-colab
    
    For some reason, soemtimes you need to wait ~30 seconds and/or run this code twice for Tensorboard to successfully 
    be piped through to a public url. 
    '''

    port = 8008
    # Close tensorboard port if it's already hosting something else
    port_out = subprocess.Popen(f'sudo lsof -i :{port}',
                                shell=True,
                                stdout=subprocess.PIPE)
    port_out = [j.decode('utf-8') for j in port_out.stdout.readlines()][1::]
    pids_on_port = [int(i.split(' ')[1]) for i in port_out if len(i) > 1]
    for pid in pids_on_port:
        p = Process(pid)
        p.terminate()

    LOG_DIR = '../../models/logs/20210122-204141'

    # Define commands to initialize tensorboard + ngrok, then set up a tunnerl
    cmd_str_tb = f'tensorboard --logdir {LOG_DIR} --host 0.0.0.0 --port {port} &'
    cmd_str_ngrok = f'~/ngrok http {port} &'
    cmd_str_pipe = 'curl -s http://localhost:4040/api/tunnels'

    # Run commands
    subprocess.Popen(cmd_str_tb, shell=True, stdout=subprocess.PIPE)
    subprocess.Popen(cmd_str_ngrok, shell=True, stdout=subprocess.PIPE)
    cmd_out = subprocess.Popen(cmd_str_pipe,
                               shell=True,
                               stdout=subprocess.PIPE)
示例#19
0
    while pid_exists(PID):
        proc = Process(PID)
        timeout = 5
        try:
            print(f"[{timeout} seconds] Trying to kill process PID: {PID}")
            proc.kill()
            proc.wait(timeout)
        except:
            pass
        finally:
            if pid_exists(PID):
                try:
                    print(
                        f"[{timeout} seconds] Trying to kill process PID: {PID}"
                    )
                    proc.terminate()
                    proc.wait(timeout)
                except:
                    print(f"Process is out of controll PID: {PID}")

    if stderr is not None:
        stdout.close()

    if not args.leave:
        from shutil import rmtree
        rmtree(work_dir)
        print("deleted: {}".format(work_dir))
    else:
        print("datadir not deleted: {}".format(work_dir))

    exit(RETCODE)
示例#20
0
def terminate_process_group(pid):
    '''Send sigterm to a process id and its children'''
    p = Process(pid)
    for child in p.children(recursive=True):
        child.terminate()
    p.terminate()
示例#21
0
 def _stop_worker_process(self, process: psutil.Process):
     """ Terminate a new worker node and remove it from the process pool. """
     process.terminate()
     process.wait(timeout=60)
     self.job_queue_size -= 1
     self._processes.remove(process)
示例#22
0
 def _stop_worker_process(self, process: psutil.Process):
     """ Terminate a new worker node and remove it from the process pool. """
     process.terminate()
     self._processes.remove(process)
示例#23
0
 def Terminate(self):
     p = Process(self.pid)
     return p.terminate()