how to kill process and child processes from python?

If the parent process is not a “process group” but you want to kill it with the children, you can use psutil (https://psutil.readthedocs.io/en/latest/#processes). os.killpg cannot identify pid of a non-process-group.

import psutil

parent_pid = 30437   # my example
parent = psutil.Process(parent_pid)
for child in parent.children(recursive=True):  # or parent.children() for recursive=False
    child.kill()
parent.kill()

Leave a Comment