Killing sudo subprocess in python

import os
import subprocess
import signal


def kill_on_exit(pid):
    pgid = os.getpgid(pid)

    def kill_child(signalNumber, frame):
        print("Killing child process %s", pgid)
        subprocess.check_call(["sudo", "kill", str(pgid)])
        os.waitpid(pgid, 0)

    signal.signal(signal.SIGINT, kill_child)  # ctrl + c
    signal.signal(signal.SIGTERM, kill_child)


proc = subprocess.Popen(
    ["sudo", "sleep", "600"],
    stderr=subprocess.PIPE, preexec_fn=os.setpgrp
)
kill_on_exit(proc.pid)
proc.wait()