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

Question:

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", "-l"], stderr=subprocess.STDOUT)

The problem is that even if the parent process dies, the child is still running.
Is there any way I can kill the child process as well when the parent dies?

Asked By: Clara

||

Answers:

Manually you could do this:

ps aux | grep <process name>

get the PID(second column) and

kill -9 <PID>
-9 is to force killing it

Answered By: Ciprian Tarta

Don’t know the specifics, but the best way is still to catch errors (and perhaps even all errors) with signal and terminate any remaining processes there.

import signal
import sys
import subprocess
import os

def signal_handler(signal, frame):
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

a = subprocess.check_output(["ls", "-l"], stderr=subprocess.STDOUT)

while 1:
    pass # Press Ctrl-C (breaks the application and is catched by signal_handler()

This is just a mockup, you’d need to catch more than just SIGINT but the idea might get you started and you’d need to check for spawned process somehow still.

http://docs.python.org/2/library/os.html#os.kill
http://docs.python.org/2/library/subprocess.html#subprocess.Popen.pid
http://docs.python.org/2/library/subprocess.html#subprocess.Popen.kill

I’d recommend rewriting a personalized version of check_output cause as i just realized check_output is really just for simple debugging etc since you can’t interact so much with it during executing..

Rewrite check_output:

from subprocess import Popen, PIPE, STDOUT
from time import sleep, time

def checkOutput(cmd):
    a = Popen('ls -l', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    print(a.pid)
    start = time()
    while a.poll() == None or time()-start <= 30: #30 sec grace period
        sleep(0.25)
    if a.poll() == None:
        print('Still running, killing')
        a.kill()
    else:
        print('exit code:',a.poll())
    output = a.stdout.read()
    a.stdout.close()
    a.stdin.close()
    return output

And do whatever you’d like with it, perhaps store the active executions in a temporary variable and kill them upon exit with signal or other means of intecepting errors/shutdowns of the main loop.

In the end, you still need to catch terminations in the main application in order to safely kill any childs, the best way to approach this is with try & except or signal.

Answered By: Torxed

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))
Answered By: micromoses

Your problem is with using subprocess.check_output – you are correct, you can’t get the child PID using that interface. Use Popen instead:

proc = subprocess.Popen(["ls", "-l"], stdout=PIPE, stderr=PIPE)

# Here you can get the PID
global child_pid
child_pid = proc.pid

# Now we can wait for the child to complete
(output, error) = proc.communicate()

if error:
    print "error:", error

print "output:", output

To make sure you kill the child on exit:

import os
import signal
def kill_child():
    if child_pid is None:
        pass
    else:
        os.kill(child_pid, signal.SIGTERM)

import atexit
atexit.register(kill_child)
Answered By: cdarke

As of Python 3.2 there is a ridiculously simple way to do this:

from subprocess import Popen

with Popen(["sleep", "60"]) as process:
    print(f"Just launched server with PID {process.pid}")

I think this will be best for most use cases because it’s simple and portable, and it avoids any dependence on global state.

If this solution isn’t powerful enough, then I would recommend checking out the other answers and discussion on this question or on Python: how to kill child process(es) when parent dies?, as there are a lot of neat ways to approach the problem that provide different trade-offs around portability, resilience, and simplicity.

Answered By: paulkernfeld
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.