How to kill a python child process created with subprocess.check_output() when the parent dies?

后端 未结 5 1836
再見小時候
再見小時候 2020-11-27 05:17

I am running on a linux machine a python script which creates a child process using subprocess.check_output() as it follows:

subprocess.check_output([\"ls\",         


        
5条回答
  •  心在旅途
    2020-11-27 06:16

    Yes, you can achieve this by two methods. Both of them require you to use Popen instead of check_output. The first is a simpler method, using try..finally, as follows:

    from contextlib import contextmanager
    
    @contextmanager
    def run_and_terminate_process(*args, **kwargs):
    try:
        p = subprocess.Popen(*args, **kwargs)
        yield p        
    finally:
        p.terminate() # send sigterm, or ...
        p.kill()      # send sigkill
    
    def main():
        with run_and_terminate_process(args) as running_proc:
            # Your code here, such as running_proc.stdout.readline()
    

    This will catch sigint (keyboard interrupt) and sigterm, but not sigkill (if you kill your script with -9).

    The other method is a bit more complex, and uses ctypes' prctl PR_SET_PDEATHSIG. The system will send a signal to the child once the parent exits for any reason (even sigkill).

    import signal
    import ctypes
    libc = ctypes.CDLL("libc.so.6")
    def set_pdeathsig(sig = signal.SIGTERM):
        def callable():
            return libc.prctl(1, sig)
        return callable
    p = subprocess.Popen(args, preexec_fn = set_pdeathsig(signal.SIGTERM))
    

提交回复
热议问题