Python3 笔记 在Python3里面跑命令并且Ctrl+C的时候可以Kill该命令及其所有的子进程 01world/tools/libs.py Lines 1 to 11 in 433d92c import subprocess, sys, os def run_sync (cmd): print(f"Run {cmd}") process = subprocess.Popen(cmd, shell=True, start_new_session=True) try: process.wait() except KeyboardInterrupt: print(f"Kill subprocess {process.pid} and it's children.") os.system(f"pkill --session {process.pid}") sys.exit(1)