How to terminate a python subprocess launched with shell=True

前端 未结 12 2694
轮回少年
轮回少年 2020-11-21 04:53

I\'m launching a subprocess with the following command:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

However, when I try t

12条回答
  •  囚心锁ツ
    2020-11-21 05:07

    what i feel like we could use:

    import os
    import signal
    import subprocess
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    
    os.killpg(os.getpgid(pro.pid), signal.SIGINT)
    

    this will not kill all your task but the process with the p.pid

提交回复
热议问题