简体   繁体   中英

How to stop a python script launching many subprocesses from terminal

I have a python script which launches a sequence of subprocesses in a loop:

for c in cmds:
  subprocess.call(c, shell=True)

When the script is running in terminal, I try to stop it with Ctrl + C or Ctrl + D , however, it will continue to launch next subprocess. How can I terminate the whole python script at once?

What likely happens is that ctrl-C is intercepted by you sub-process (running on the same terminal) and never gets to your Python script. What you can do is to check the return value of subprocess.call to see whether the child process was killed by a signal and decide whether you want to stop submitting new processes.

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