简体   繁体   English

python守护进程中的子子进程终止

[英]child subprocess kill in python daemon

I have damon in python which runs external program: 我在python运行外部程序:

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

when I kill daemon, the child process (java) is still running 当我杀死守护进程时,子进程(java)仍在运行

how can I make so that child process is also killed ? 我怎么能让孩子的过程也被杀死?

Use subprocess.Popen() instead of subprocess.call() . 使用subprocess.Popen()而不是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: 要捕获kill信号,你可以这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM