code in fork_child.py
from subprocess import Popen
child = Popen(["ping", "google.com"], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = child.communicate()
I run it from a terminal window as -
$python fork_child.py
From another terminal window if I get the PID of fork_child.py and kill it with SIGTERM, "ping" doesn't get killed. How do I make sure that ping too gets killed when fork_child receives a SIGTERM ?
Children don't automatically die when the parent process is killed. They die if:
The signals
module contains examples how to write a signal handler.
So you need to:
child.terminate()
followed by child.wait()
The wait()
is necessary to allow the OS to garbage collect the child process. If you forget it, you may end up with zombie processes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With