Python os.getpgid() Examples

The following are 30 code examples of os.getpgid(). 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 os , or try the search function .
Example #1
Source File: __init__.py    From aetros-cli with MIT License 10 votes vote down vote up
def raise_sigint():
    """
    Raising the SIGINT signal in the current process and all sub-processes.

    os.kill() only issues a signal in the current process (without subprocesses).
    CTRL+C on the console sends the signal to the process group (which we need).
    """
    if hasattr(signal, 'CTRL_C_EVENT'):
        # windows. Need CTRL_C_EVENT to raise the signal in the whole process group
        os.kill(os.getpid(), signal.CTRL_C_EVENT)
    else:
        # unix.
        pgid = os.getpgid(os.getpid())
        if pgid == 1:
            os.kill(os.getpid(), signal.SIGINT)
        else:
            os.killpg(os.getpgid(os.getpid()), signal.SIGINT) 
Example #2
Source File: start.py    From cf-mendix-buildpack with Apache License 2.0 8 votes vote down vote up
def terminate_process():
    if m2ee:
        logging.info("stopping app...")
        if not m2ee.stop():
            if not m2ee.terminate():
                m2ee.kill()
    try:
        this_process = os.getpgid(0)
        logging.debug(
            "Terminating process group with pgid=%s", format(this_process)
        )
        os.killpg(this_process, signal.SIGTERM)
        time.sleep(3)
        os.killpg(this_process, signal.SIGKILL)
    except OSError:
        logging.exception("Failed to terminate all child processes") 
Example #3
Source File: managers.py    From automl-phase-2 with MIT License 6 votes vote down vote up
def orphan_finder(self):
        # Lists all the processes and finds ones in our group with parent 'init'
        # for ps in psutil.process_iter():
        #     if os.getpgid(ps.pid) == self.pgid:
        #         if ps.parent().name() == 'init':
        #             logger.error("Orphaned child with PID %d", ps.pid)
        #             ps.terminate()

        # More efficient - go through pids in tasks list
        with open('/sys/fs/cgroup/memory/{}/memory.usage_in_bytes'.format(self.cgroup), 'rb') as fp:
            for line in fp:
                pid = line.strip()
                if not pid:
                    continue
                pid = int(pid)
                try:
                    ps = psutil.Process(pid=pid)
                    if ps.parent().name() == 'init':
                        logger.error("Orphaned child with PID %d", pid)
                        ps.terminate()
                except (psutil.NoSuchProcess, psutil.AccessDenied, IOError):
                    pass 
Example #4
Source File: timeshare.py    From TikZ with GNU General Public License v3.0 6 votes vote down vote up
def execute(self,dt):
        if self.finished: return "finished"
        if not self.running:
            self.process = Process(target = executeInProcessGroup, args = (self,))
            self.process.start()
            print "timeshare child PID:",self.process.pid
            os.setpgid(self.process.pid,self.process.pid)
            print "timeshare process group",os.getpgid(self.process.pid)
            assert os.getpgid(self.process.pid) == self.process.pid
            print "my process group",os.getpgrp(),"which should be",os.getpgid(0)
            assert os.getpgid(self.process.pid) != os.getpgid(0)
            self.running = True
        else:
            os.killpg(self.process.pid, signal.SIGCONT)
        
        self.process.join(dt)
        if self.process.is_alive():
            os.killpg(self.process.pid, signal.SIGSTOP)
            return "still running"
        else:
            self.finished = True
            return self.q.get() 
Example #5
Source File: managers.py    From automl-phase-2 with MIT License 6 votes vote down vote up
def __init__(self, overhead_memory=2**30, cgroup_soft_limit=6 * 2 ** 30, cgroup_hard_limit=2 * 2 ** 30,
                 **kwargs):
        super(MemoryManager, self).__init__(**kwargs)

        # Create variables that will be set by future managers
        self.meta_learners = []
        # Private memory - physical memory that is only accesible to the process (e.g. no shared libs or arrays)
        self.child_private_memory = dict()
        self.learner_preference = []
        self.pid = os.getpid()
        self.pgid = os.getpgid(0)

        self.cgroup = 'backstreet_bayes_learners'
        # As long as program exits properly control group will be deleted after use
        self.cgroup_mem_limit = None  # limit for cgroup - set during first action
        self.overhead_memory = overhead_memory  # cgroup is limited to available mem - overhead
        self.cgroup_soft_limit = cgroup_soft_limit  # amount required inside the cgroup
        self.cgroup_hard_limit = cgroup_hard_limit  # Point at which emergency pause occurs

        self.password = os.environ.get('PW', '')  # Required to create and manage control groups 
Example #6
Source File: test_subprocess.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_start_new_session(self):
        # For code coverage of calling setsid().  We don't care if we get an
        # EPERM error from it depending on the test execution environment, that
        # still indicates that it was called.
        try:
            output = subprocess.check_output(
                    [sys.executable, "-c",
                     "import os; print(os.getpgid(os.getpid()))"],
                    start_new_session=True)
        except OSError as e:
            if e.errno != errno.EPERM:
                raise
        else:
            parent_pgid = os.getpgid(os.getpid())
            child_pgid = int(output)
            self.assertNotEqual(parent_pgid, child_pgid) 
Example #7
Source File: test_subprocess.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_start_new_session(self):
        # For code coverage of calling setsid().  We don't care if we get an
        # EPERM error from it depending on the test execution environment, that
        # still indicates that it was called.
        try:
            output = subprocess.check_output(
                    [sys.executable, "-c",
                     "import os; print(os.getpgid(os.getpid()))"],
                    start_new_session=True)
        except OSError as e:
            if e.errno != errno.EPERM:
                raise
        else:
            parent_pgid = os.getpgid(os.getpid())
            child_pgid = int(output)
            self.assertNotEqual(parent_pgid, child_pgid) 
Example #8
Source File: test_subprocess.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_start_new_session(self):
        # For code coverage of calling setsid().  We don't care if we get an
        # EPERM error from it depending on the test execution environment, that
        # still indicates that it was called.
        try:
            output = subprocess.check_output(
                    [sys.executable, "-c",
                     "import os; print(os.getpgid(os.getpid()))"],
                    start_new_session=True)
        except OSError as e:
            if e.errno != errno.EPERM:
                raise
        else:
            parent_pgid = os.getpgid(os.getpid())
            child_pgid = int(output)
            self.assertNotEqual(parent_pgid, child_pgid) 
Example #9
Source File: common.py    From marsnake with GNU General Public License v3.0 6 votes vote down vote up
def is_kernel_thread(proc):
	if is_linux():
		"""Return True if proc is a kernel thread, False instead."""
		try:
			return os.getpgid(proc.pid) == 0
		# Python >= 3.3 raises ProcessLookupError, which inherits OSError
		except OSError:
			# return False is process is dead
			return False

	elif is_windows():
		return proc.pid == 0 or proc.pid == 4

	return False

#5.2 M 
Example #10
Source File: cgroups.py    From macke with Apache License 2.0 6 votes vote down vote up
def cgroups_run_timed_subprocess(command, *args, cgroup=None, timeout=1, **kwargs):
    """
    Starts a subprocess, waits for it and returns (exitcode, output, erroutput)
    """
    if cgroup is None:
        raise ValueError("No cgroup given")
    p = cgroups_Popen(command, *args, cgroup=cgroup, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=setsid,
                      **kwargs)
    output = b""
    err = b""
    try:
        while p.poll() is None:
            (o, e) = p.communicate(None, timeout=timeout)
            output += o
            err += e
    # On hangup kill the program (and children)
    except subprocess.TimeoutExpired:
        killpg(getpgid(p.pid), signal.SIGKILL)

    return p.returncode, output, err 
Example #11
Source File: Fuzzer.py    From macke with Apache License 2.0 6 votes vote down vote up
def _run_subprocess(*args, **kwargs):
    """
    Starts a subprocess, waits for it and returns (exitcode, output, erroutput)
    """
    p = subprocess.Popen(*args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=setsid, **kwargs)
    output = b""
    err = b""
    try:
        while p.poll() is None:
            (o, e) = p.communicate(None, timeout=1)
            output += o
            err += e
    # On hangup kill the program (and children)
    except subprocess.TimeoutExpired:
        killpg(getpgid(p.pid), signal.SIGKILL)

    return p.returncode, output, err 
Example #12
Source File: control.py    From civet with Apache License 2.0 6 votes vote down vote up
def kill_proc(self, proc):
        """
        Stop the process if it is alive.
        Input:
          proc: dict: as created in start_proc()
        Return:
          str: information on what happened
        """
        if proc["process"].poll() == None:
            try:
                pgid = os.getpgid(proc["process"].pid)
                os.killpg(pgid, signal.SIGTERM)
            except:
                """
                Could already be dead
                """
            proc["process"].terminate()
            proc["runtime"] = time.time() - proc["start"]
            proc["running"] = False
            return "Process %s killed" % proc["process"].pid
        return "" 
Example #13
Source File: submitted_run.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def cancel(self):
        # Interrupt child process if it hasn't already exited
        if self.command_proc.poll() is None:
            # Kill the the process tree rooted at the child if it's the leader of its own process
            # group, otherwise just kill the child
            try:
                if self.command_proc.pid == os.getpgid(self.command_proc.pid):
                    os.killpg(self.command_proc.pid, signal.SIGTERM)
                else:
                    self.command_proc.terminate()
            except OSError:
                # The child process may have exited before we attempted to terminate it, so we
                # ignore OSErrors raised during child process termination
                _logger.info(
                    "Failed to terminate child process (PID %s) corresponding to MLflow "
                    "run with ID %s. The process may have already exited.",
                    self.command_proc.pid, self._run_id)
            self.command_proc.wait() 
Example #14
Source File: sandbox.py    From catnip with Apache License 2.0 6 votes vote down vote up
def _FindLeadProcess(self):
    sudo_pid = os.getpgid(0)
    self._log.info('sudo_pid: %d', sudo_pid)
    ssh_pid = self._GetParentPID(sudo_pid)
    self._log.info('ssh_pid: %d', ssh_pid)
    if not ssh_pid:
      raise InternalError('Could not find the lead process')
    try:
      with open('/proc/%d/cmdline' % ssh_pid) as f:
        ssh_cmdline = f.read()
    except IOError:
      raise InternalError('Could not find the lead process')
    self._log.info('ssh_cmdline: %s', ssh_cmdline)
    if not ssh_cmdline.startswith('sshd: '):
      if self._params.debug:
        self._log.info('skipping SSH invocation check by --debug')
        return None
      raise InternalError('Not invoked from SSH')
    return sudo_pid 
Example #15
Source File: test_standard_task_runner.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_start_and_terminate_run_as_user(self):
        local_task_job = mock.Mock()
        local_task_job.task_instance = mock.MagicMock()
        local_task_job.task_instance.run_as_user = getpass.getuser()
        local_task_job.task_instance.command_as_list.return_value = [
            'airflow', 'tasks', 'test', 'test_on_kill', 'task1', '2016-01-01'
        ]

        runner = StandardTaskRunner(local_task_job)

        runner.start()
        time.sleep(0.5)

        pgid = os.getpgid(runner.process.pid)
        self.assertGreater(pgid, 0)
        self.assertNotEqual(pgid, os.getpgid(0), "Task should be in a different process group to us")

        processes = list(self._procs_in_pgroup(pgid))

        runner.terminate()

        for process in processes:
            self.assertFalse(psutil.pid_exists(process.pid), "{} is still alive".format(process))

        self.assertIsNotNone(runner.return_code()) 
Example #16
Source File: test_standard_task_runner.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_start_and_terminate(self):
        local_task_job = mock.Mock()
        local_task_job.task_instance = mock.MagicMock()
        local_task_job.task_instance.run_as_user = None
        local_task_job.task_instance.command_as_list.return_value = [
            'airflow', 'tasks', 'test', 'test_on_kill', 'task1', '2016-01-01'
        ]

        runner = StandardTaskRunner(local_task_job)
        runner.start()
        time.sleep(0.5)

        pgid = os.getpgid(runner.process.pid)
        self.assertGreater(pgid, 0)
        self.assertNotEqual(pgid, os.getpgid(0), "Task should be in a different process group to us")

        processes = list(self._procs_in_pgroup(pgid))

        runner.terminate()

        for process in processes:
            self.assertFalse(psutil.pid_exists(process.pid), "{} is still alive".format(process))

        self.assertIsNotNone(runner.return_code()) 
Example #17
Source File: webscreenshot.py    From webscreenshot with GNU Lesser General Public License v3.0 6 votes vote down vote up
def kill_em_all(sig, frame):
    """
        Terminate all processes while capturing a SIGINT from the user
    """
    logger_gen.info('CTRL-C received, exiting')
    if is_windows():
        multiprocessing.sys.exit(1)
    
    else:
        pid = os.getpid()
        pgid = os.getpgid(pid)
        sid = os.getsid(os.getpid())
        
        # if launched with --no-xserver
        if pid == sid:
            os.killpg(pgid, signal.SIGKILL)
        else:
            time.sleep(4)
            multiprocessing.sys.exit(1) 
Example #18
Source File: pipeline.py    From Comparative-Annotation-Toolkit with Apache License 2.0 6 votes vote down vote up
def _setPgid(pid, pgid):
    """set pgid of a process, ignored exception caused by race condition
    that occurs if already set by parent or child has already existed"""
    # Should just ignore on EACCES, as to handle race condition with parent
    # and child.  However some Linux kernels (seen in 2.6.18-53) report ESRCH
    # or EPERM.  To handle this is a straight-forward way, just check that the
    # change has been made.  However, in some cases the change didn't take,
    # retrying seems to make the problem go away.
    for i in range(0,5):
        try:
            os.setpgid(pid, pgid)
            return
        except OSError:
            if os.getpgid(pid) == pgid:
                return
            time.sleep(0.25) # sleep for retry
    # last try, let it return an error
    os.setpgid(pid, pgid)

# FIXME: why not use pipes.quote? 
Example #19
Source File: carla_env.py    From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License 5 votes vote down vote up
def clear_server_state(self):
        print("Clearing Carla server state")
        try:
            if self.client:
                self.client.disconnect()
                self.client = None
        except Exception as e:
            print("Error disconnecting client: {}".format(e))
            pass
        if self.server_process:
            pgid = os.getpgid(self.server_process.pid)
            os.killpg(pgid, signal.SIGKILL)
            live_carla_processes.remove(pgid)
            self.server_port = None
            self.server_process = None 
Example #20
Source File: carla_env.py    From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License 5 votes vote down vote up
def init_server(self):
        print("Initializing new Carla server...")
        # Create a new server process and start the client.
        self.server_port = random.randint(10000, 60000)
        if self.config["render"]:
            self.server_process = subprocess.Popen(
                [SERVER_BINARY, self.config["server_map"],
                 "-windowed", "-ResX=400", "-ResY=300",
                 "-carla-server",
                 "-carla-world-port={}".format(self.server_port)],
                preexec_fn=os.setsid, stdout=open(os.devnull, "w"))
        else:
            self.server_process = subprocess.Popen(
                ("SDL_VIDEODRIVER=offscreen SDL_HINT_CUDA_DEVICE={} {} " +
                 self.config["server_map"] + " -windowed -ResX=400 -ResY=300"
                 " -carla-server -carla-world-port={}").format(0, SERVER_BINARY, self.server_port),
                shell=True, preexec_fn=os.setsid, stdout=open(os.devnull, "w"))

        live_carla_processes.add(os.getpgid(self.server_process.pid))

        for i in range(RETRIES_ON_ERROR):
            try:
                self.client = CarlaClient("localhost", self.server_port)
                return self.client.connect()
            except Exception as e:
                print("Error connecting: {}, attempt {}".format(e, i))
                time.sleep(2) 
Example #21
Source File: carla_env.py    From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License 5 votes vote down vote up
def init_server(self):
        print("Initializing new Carla server...")
        # Create a new server process and start the client.
        self.server_port = random.randint(10000, 60000)
        if self.config["render"]:
            self.server_process = subprocess.Popen(
                [SERVER_BINARY, self.config["server_map"],
                 "-windowed", "-ResX=400", "-ResY=300",
                 "-carla-server",
                 "-carla-world-port={}".format(self.server_port)],
                preexec_fn=os.setsid, stdout=open(os.devnull, "w"))
        else:
            self.server_process = subprocess.Popen(
                ("SDL_VIDEODRIVER=offscreen SDL_HINT_CUDA_DEVICE={} {} " +
                 self.config["server_map"] + " -windowed -ResX=400 -ResY=300"
                 " -carla-server -carla-world-port={}").format(0, SERVER_BINARY, self.server_port),
                shell=True, preexec_fn=os.setsid, stdout=open(os.devnull, "w"))

        live_carla_processes.add(os.getpgid(self.server_process.pid))

        for i in range(RETRIES_ON_ERROR):
            try:
                self.client = CarlaClient("localhost", self.server_port)
                return self.client.connect()
            except Exception as e:
                print("Error connecting: {}, attempt {}".format(e, i))
                time.sleep(2) 
Example #22
Source File: utils.py    From cmd2 with MIT License 5 votes vote down vote up
def send_sigint(self) -> None:
        """Send a SIGINT to the process similar to if <Ctrl>+C were pressed"""
        import signal
        if sys.platform.startswith('win'):
            # cmd2 started the Windows process in a new process group. Therefore
            # a CTRL_C_EVENT can't be sent to it. Send a CTRL_BREAK_EVENT instead.
            self._proc.send_signal(signal.CTRL_BREAK_EVENT)
        else:
            # Since cmd2 uses shell=True in its Popen calls, we need to send the SIGINT to
            # the whole process group to make sure it propagates further than the shell
            try:
                group_id = os.getpgid(self._proc.pid)
                os.killpg(group_id, signal.SIGINT)
            except ProcessLookupError:
                return 
Example #23
Source File: carla_env.py    From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License 5 votes vote down vote up
def clear_server_state(self):
        print("Clearing Carla server state")
        try:
            if self.client:
                self.client.disconnect()
                self.client = None
        except Exception as e:
            print("Error disconnecting client: {}".format(e))
            pass
        if self.server_process:
            pgid = os.getpgid(self.server_process.pid)
            os.killpg(pgid, signal.SIGKILL)
            live_carla_processes.remove(pgid)
            self.server_port = None
            self.server_process = None 
Example #24
Source File: protocols.py    From supriya with MIT License 5 votes vote down vote up
def quit(self):
        if not self.is_running:
            return
        process_group = os.getpgid(self.process.pid)
        os.killpg(process_group, signal.SIGINT)
        self.process.terminate()
        self.process.wait()
        self.is_running = False 
Example #25
Source File: selflog_adv.py    From Pigrow with GNU General Public License v3.0 5 votes vote down vote up
def check_script_running(script):
    try:
        script_test = map(int,check_output(["pidof",script,"-x"]).split())
    except:
        script_test = False
    if script_test == False:
        #print(script + " not running!")
        return {'num_running':'0','script_status':'none','script_path':'none'}
    else:
        if len(script_test) > 1:
            #print("There's more than one " + script + " running!")
            for pid in script_test:
                #print "---"
                #print pid
                try:
                    script_test_path = open(os.path.join('/proc', str(pid), 'cmdline'), 'rb').read()
                    #print script_test_path
                except IOError:
                    #print("I think it died when we looked at it...")
                    return {'num_running':'0','script_status':'died','script_path':'none'}
                #print os.getpgid(pid) # Return the process group id
                for line in open("/proc/"+ str(pid)  +"/status").readlines():
                    if line.split(':')[0] == "State":
                        script_test_status = line.split(':')[1].strip()
                return {'num_running':str(len(script_test)),'script_status':script_test_status,'script_path':script_test_path}
                #os.kill(pid, sig)
        else:
            #print(script + " is running!")
            for line in open("/proc/"+ str(script_test[0])  +"/status").readlines():
                if line.split(':')[0] == "State":
                    script_test_status = line.split(':')[1].strip()
            try:
                script_test_path = open(os.path.join('/proc', str(script_test[0]), 'cmdline'), 'rb').read()
            except IOError:
                #print("I think it died when we looked at it...")
                return {'num_running':'0','script_status':'died','script_path':'none'}
            #print script_test_path
            #print script_test_status
            return {'num_running':'1','script_status':script_test_status,'script_path':script_test_path} 
Example #26
Source File: protocols.py    From supriya with MIT License 5 votes vote down vote up
def boot(self, options, scsynth_path, port):
        if self.is_running:
            return
        options_string = options.as_options_string(port)
        command = "{} {}".format(scsynth_path, options_string)
        logger.info("Boot: {}".format(command))
        self.process = subprocess.Popen(
            command,
            shell=True,
            stderr=subprocess.STDOUT,
            stdout=subprocess.PIPE,
            start_new_session=True,
        )
        try:
            start_time = time.time()
            timeout = 10
            while True:
                line = self.process.stdout.readline().decode().rstrip()
                if line:
                    logger.info("Boot: {}".format(line))
                if line.startswith("SuperCollider 3 server ready"):
                    break
                elif line.startswith("ERROR:"):
                    raise supriya.exceptions.ServerCannotBoot(line)
                elif line.startswith(
                    "Exception in World_OpenUDP: bind: Address already in use"
                ):
                    raise supriya.exceptions.ServerCannotBoot(line)
                elif (time.time() - start_time) > timeout:
                    raise supriya.exceptions.ServerCannotBoot(line)
            self.is_running = True
        except supriya.exceptions.ServerCannotBoot:
            try:
                process_group = os.getpgid(self.process.pid)
                os.killpg(process_group, signal.SIGINT)
                self.process.terminate()
                self.process.wait()
            except ProcessLookupError:
                pass
            raise 
Example #27
Source File: Lg_mafft.py    From PhyloSuite with GNU General Public License v3.0 5 votes vote down vote up
def closeEvent(self, event):
        self.guiSave()
        self.log_gui.close()  # 关闭子窗口
        self.closeSig.emit("MAFFT", self.fetchWorkflowSetting())
        # 断开showSig和closeSig的槽函数连接
        try:
            self.showSig.disconnect()
        except:
            pass
        try:
            self.closeSig.disconnect()
        except:
            pass
        if self.workflow:
            self.ui_closeSig.emit("MAFFT")
            # 自动跑的时候不杀掉程序
            return
        if self.isRunning():
            reply = QMessageBox.question(
                self,
                "MAFFT",
                "<p style='line-height:25px; height:25px'>MAFFT is still running, terminate it?</p>",
                QMessageBox.Yes,
                QMessageBox.Cancel)
            if reply == QMessageBox.Yes:
                try:
                    self.worker.stopWork()
                    if platform.system().lower() in ["windows", "darwin"]:
                        self.mafft_popen.kill()
                    else:
                        os.killpg(os.getpgid(self.mafft_popen.pid), signal.SIGTERM)
                    self.mafft_popen = None
                    self.mafft_interrupt = True
                except:
                    self.mafft_popen = None
                    self.mafft_interrupt = True
            else:
                event.ignore() 
Example #28
Source File: multi_env.py    From macad-gym with MIT License 5 votes vote down vote up
def _clear_server_state(self):
        """Clear server process"""

        print("Clearing Carla server state")
        try:
            if self._client:
                self._client = None
        except Exception as e:
            print("Error disconnecting client: {}".format(e))
        if self._server_process:
            pgid = os.getpgid(self._server_process.pid)
            os.killpg(pgid, signal.SIGKILL)
            live_carla_processes.remove(pgid)
            self._server_port = None
            self._server_process = None 
Example #29
Source File: encode_lib_common.py    From chip-seq-pipeline2 with MIT License 5 votes vote down vote up
def run_shell_cmd(cmd):
    p = subprocess.Popen(
        ['/bin/bash', '-o', 'pipefail'],  # to catch error in pipe
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
        preexec_fn=os.setsid)  # to make a new process with a new PGID
    pid = p.pid
    pgid = os.getpgid(pid)
    log.info('run_shell_cmd: PID={}, PGID={}, CMD={}'.format(pid, pgid, cmd))
    stdout, stderr = p.communicate(cmd)
    rc = p.returncode
    err_str = 'PID={}, PGID={}, RC={}\nSTDERR={}\nSTDOUT={}'.format(
        pid, pgid, rc, stderr.strip(), stdout.strip())
    if rc:
        # kill all child processes
        try:
            os.killpg(pgid, signal.SIGKILL)
        except:
            pass
        finally:
            raise Exception(err_str)
    else:
        log.info(err_str)
    return stdout.strip('\n')

# math 
Example #30
Source File: Klee.py    From macke with Apache License 2.0 5 votes vote down vote up
def _check_output(command, cwd, timeout):
    proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd, preexec_fn=setsid)
    try:
        output, _ = proc.communicate(None, timeout=timeout)
    except subprocess.TimeoutExpired:
        killpg(getpgid(proc.pid), signal.SIGKILL)
        # timeout for sanity check
        output, _ = proc.communicate(None, timeout=timeout)
        raise subprocess.TimeoutExpired(proc.args, timeout, output=output)

    retcode = proc.poll()

    if retcode:
        raise subprocess.CalledProcessError(retcode, proc.args, output=output)
    return output