Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess termination in python

I have a Python script which starts a subprocess another Python script using subprocess.Popen(). This subprocess starts another subprocess( another Python script) using Popen. Script A calls script B which calls script C. If I kill process script B using os.kill() will it terminate the process running script C. If not is there any way to do that.

like image 636
jatin arora Avatar asked Dec 05 '25 16:12

jatin arora


1 Answers

As it stands, if script A kills B using os.kill then C will not itself be killed.

In order to ensure this, script B could take care of killing C when it exits

# this is in script B
import functools, atexit

def kill_children(*pids):
    import os, signal

    for pid in pids or []:
        os.kill(pid, signal.SIGTERM)

# we start a process for C
c_pid = ...

# kill C when we we exit
atexit.register(functools.partial(kill_children, c_pid)) 
like image 117
donkopotamus Avatar answered Dec 08 '25 05:12

donkopotamus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!