jatin arora
jatin arora

Reputation: 33

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.

Upvotes: 3

Views: 422

Answers (1)

donkopotamus
donkopotamus

Reputation: 23236

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)) 

Upvotes: 2

Related Questions