Kill the running process and remove the PID/lock file.
175614
def kill(self):
        
        try:
            kill(int(self.get()), 9)
            
        # Failed to kill the process
        except Exception as e:
            self.die('Failed to kill process: {}'.format(str(e)))
            
        # Remove the stale PID file
        self.remove()
974376
def kill(path):
    
    # try to read the pid from the pidfile
    pid = read(path)
    if pid is None:
        return

    # Try to kill the process.
    logging.info("Killing PID %s", pid)
    try:
        os.kill(pid, 9)
    except OSError as xxx_todo_changeme2:
        # re-raise if the error wasn't "No such process"
        (code, text) = xxx_todo_changeme2.args
        # re-raise if the error wasn't "No such process"
        if code != errno.ESRCH:
            raise
899240
def kill_after(seconds, timeout=2):
    """"""
    pid = os.getpid()
    kill = os.kill
    run_after_async(seconds, kill, pid, signal.SIGTERM)
    run_after_async(seconds + timeout, kill, pid, 9)
956825
def cleanup(self):
        """"""
        try:
            os.remove(self.pid_file)
        except OSError as exc:
            logger.critical("Could not remove pid_file: %s", exc)

        # Save the final status of the flow.
        self.flow.pickle_dump()
425699
def _kill_process(self, pid, sig=signal.SIGKILL):
        """"""
        try:
            os.kill(pid, sig)
        except OSError as e:
            if e.errno == errno.ESRCH: # process itself returned and exited before killing
                logging.debug("Failure %s while killing process %s with signal %s: %s",
                              e.errno, pid, sig, e.strerror)
            else:
                logging.warning("Failure %s while killing process %s with signal %s: %s",
                                e.errno, pid, sig, e.strerror)
551954
def daemon_stop(pidfile, sig=None):
    
    logger.debug("stop daemon application pidfile={pidfile}.".format(pidfile=pidfile))
    pid = load_pid(pidfile)
    logger.debug("load pid={pid}".format(pid=pid))
    if not pid:
        six.print_("Application is not running or crashed...", file=os.sys.stderr)
        os.sys.exit(195)
    process_kill(pid, sig)
    return pid
298347
def is_running(self):
        
        try:
            kill(int(self.pid.get()), 0)
            return True
        
        # Process not running, remove PID/lock file if it exists
        except:
            self.pid.remove()
            self.lock.remove()
            return False
1123343
def kill(self):
        

        for process in list(self.processes):
            process["subprocess"].send_signal(signal.SIGKILL)

        self.stop_watch()
586923
def __kill_if_not_shut_properly(self):
        
        import tempfile
        d = tempfile.gettempdir()
        name = os.path.join(d, "hoverpy.%i.%i"%(self._proxyPort, self._adminPort))
        if os.path.exists(name):
            logging.debug("pid file exists.. killing it")
            f = open(name, "r")
            pid = int(f.read())
            try:
                import signal
                os.kill(pid, signal.SIGTERM)
                logging.debug("killing %i"%pid)
            except:
                logging.debug("nothing to clean up")
                pass
            finally:
                os.unlink(name)
1365106
def stop(self, sig=signal.SIGTERM):
        

        if self.pid is None:
            self.pid = self.read_pid()
        if self.pid is None:
            sp = BackgroundProcess.objects.filter(pk=1).first()
            if sp:
                self.pid = sp.pid
        if self.pid is None or self.pid == 0:
            logger.error("can't determine process id exiting.")
            return False
        if self.pid != getpid():
            # calling from outside the daemon instance
            logger.debug('send sigterm to daemon')
            try:
                kill(self.pid, sig)
                return True
            except OSError as e:
                if e.errno == errno.ESRCH:
                    return False
                else:
                    return False

        logger.debug('start termination of the daemon')
        BackgroundProcess.objects.filter(pk=self.process_id).update(
            last_update=now(),
            message='stopping..')

        timeout = time() + 60  # wait max 60 seconds
        self.kill_processes(signal.SIGTERM)
        while self.PROCESSES and time() < timeout:
            self.kill_processes(signal.SIGTERM)
            sleep(1)
        self.kill_processes(signal.SIGKILL)
        BackgroundProcess.objects.filter(pk=self.process_id).update(
            last_update=now(),
            message='stopped')
        logger.debug('termination of the daemon done')
        return True
1148724
def _psutil_kill_pid(pid):
    
    try:
        parent = Process(pid)
        for child in parent.children(recursive=True):
            child.kill()
        parent.kill()
    except NoSuchProcess:
        return
173658
def _terminate_service(self):
        """"""
        with self.__lock:
            if self._service:
                self._service.terminate()
            if self._pipe_commands:
                self._pipe_commands.close()
            if self._pipe_service:
                self._pipe_service.close()
            self._pipe_commands = None
            self._pipe_service = None
            self._service_class_name = None
            self._service_name = None
            if self._service_status != CommonService.SERVICE_STATUS_TEARDOWN:
                self.update_status(status_code=CommonService.SERVICE_STATUS_END)
            if self._service:
                self._service.join()  # must wait for process to be actually destroyed
            self._service = None
426492
def stop(self, sig=None, timeout=10):
        
        if self.process is None:
            return self

        if sig is None:
            sig = self._sig_stop

        try:
            os.killpg(self.process.pid, sig)
        except OSError as err:
            if err.errno in IGNORED_ERROR_CODES:
                pass
            else:
                raise

        def process_stopped():
            """Return True only only when self.process is not running."""
            return self.running() is False

        self._set_timeout(timeout)
        try:
            self.wait_for(process_stopped)
        except TimeoutExpired:
            log.warning('Timeout expired, killing process', process=self)
            pass

        self._kill_all_kids(sig)
        self._clear_process()
        return self
980745
def stop(self):
        
        # unsetup_fuse()
        self.fuse_process.teardown()
        for uuid in self.processes:
            self.processes[uuid].terminate()
659377
def _killall(self, force=False):
        """"""
        for_termination = []

        for n, p in iteritems(self._processes):
            if 'returncode' not in p:
                for_termination.append(n)

        for n in for_termination:
            p = self._processes[n]
            signame = 'SIGKILL' if force else 'SIGTERM'
            self._system_print("sending %s to %s (pid %s)\n" %
                               (signame, n, p['pid']))
            if force:
                self._env.kill(p['pid'])
            else:
                self._env.terminate(p['pid'])
1304553
def release(self):
        
        
        if self.is_locked_by_me():
            os.remove(self.lock_filename)
            logger.debug('The lock {} is released by me (pid: {}).'.format(self.lock_filename, self.pid))
        if self.fd:
            os.close(self.fd)
            self.fd = None
1191298
def die(self):
        """"""
        if self.process:
            _log(self.logging,
                 'Stopping {0} server with PID: {1} running at {2}.'
                     .format(self.__class__.__name__, self.process.pid,
                             self.check_url))

            self._kill()