Kill child subprocess in python daemons

I have a damon in python that runs an external program:

subprocess.call(["java", "-jar", "start.jar"])

when i kill the daemon the child process (java) is still running

How can I make the child process also be killed?

+5
source share
1 answer

Use subprocess.Popen()instead subprocess.call(). For instance:

import subprocess
my_process = subprocess.Popen(['ls', '-l'])

To complete the children:

my_process.kill()

To capture the kill signal, you can do something like this:

import signal
import sys
def signal_handler(signal, frame):
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
+7
source

All Articles