简体   繁体   中英

Kill a running subprocess created by popen from mac terminal

I have a python script myscript.py

subprocess.Popen(my_command, shell=True)

I run python3 myscript.py from my terminal and now I couldn't stop the running my_command using ctrl + c . my_command will continue running. Is there anyway I can stop it?

thanks!

can use psutil

import subprocess

import psutil


def destroyed(proc_pid):
    process = psutil.Process(proc_pid)
    for proc in process.children(recursive=True):
        proc.kill()
    process.kill()


proc = subprocess.Popen(["sleep", " 1000"], shell=True)
try:
    proc.wait(timeout=5)
except subprocess.TimeoutExpired:
    destroyed(proc.pid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM