Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

child subprocess kill in python daemon

I have damon in python which runs external program:

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

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

how can I make so that child process is also killed ?

like image 826
Pydev UA Avatar asked Feb 25 '11 07:02

Pydev UA


1 Answers

Use subprocess.Popen() instead of subprocess.call(). For example:

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

To terminate the child:

my_process.kill()

To capture the kill signal, you could so something like this:

import signal
import sys
def signal_handler(signal, frame):
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
like image 90
Patrick Avatar answered Sep 28 '22 07:09

Patrick