简体   繁体   English

如何停止/终止运行的python脚本? (再来一次)

[英]How to stop/terminate a python script from running? (once again)

Context :上下文

I have a running python script.我有一个正在运行的 python 脚本。 It contains many os.system("./executableNane") calls in a loop.它在循环中包含许多os.system("./executableNane")调用。

If I press ctrl + C , it just stops the execution of the current ./executableNane and passes to the next one.如果我按ctrl + C ,它只会停止当前./executableNane的执行并传递到下一个。

Question :问题

How to stop the execution of the whole script and not only the execution of the current executable called?如何停止整个脚本的执行而不仅仅是当前调用的可执行文件的执行?

Please note that I have read carefully the question/answer here but even with kill I can kill the executable executableNane but not the whole script (that I cannot find using top ).请注意,我已经仔细阅读了此处的问题/答案但即使使用kill我也可以杀死可执行的可执行文件Nane但不能杀死整个脚本(我无法使用top找到)。

The only way I have to stop the script (without reboot the system) is to continue to press ctrl + C in a loop as well until all the tests are completed.我必须停止脚本(无需重新启动系统)的唯一方法是继续按 ctrl + C 循环,直到所有测试完成。

You can use subprocess and signal handlers to do this.您可以使用子进程信号处理程序来执行此操作。 You can also use subprocess to receive and send information via subprocess.PIPE , which you can read more about in the documentation.您还可以使用subprocess通过subprocess.PIPE接收和发送信息,您可以在文档中阅读更多信息。

The following should be a basic example of what you are looking to do:以下应该是您要执行的操作的基本示例:

import subprocess
import signal
import sys

def signal_handler(sig, frame):
    print("You pressed Ctrl+C, stopping.")
    print("Signal: {}".format(sig))
    print("Frame: {}".format(frame))

    sys.exit(123)

# Set up signal handler
signal.signal(signal.SIGINT, signal_handler)

print("Starting.")
while True:
    cmd = ['sleep', '10']
    p = subprocess.Popen(cmd)
    p.wait()
    if p.returncode != 0:
        print("Command failed.")
    else:
        print("Command worked.")

The other solution to the question (by @Quillan Kaseman) is much more elegant compared with the solution I have found.与我找到的解决方案相比,该问题的另一个解决方案(由@Quillan Kaseman 提供)要优雅得多。 All my problems are solved when I press Ctrl + Z instead of Ctrl + C .当我按Ctrl + Z而不是Ctrl + C时,我所有的问题都解决了。

Indeed, I have no idea why with Z works and with C does not.事实上,我不知道为什么Z可以工作而C不行。 (I will try to look for some details later on). (我稍后会尝试寻找一些细节)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何停止/终止 python 脚本的运行? - How to stop/terminate a python script from running? 如何停止/终止 python 脚本与 APScheduler 的 BackgroundScheduler() 一起运行 - How to stop/terminate a python script from running with BackgroundScheduler() of APScheduler 如何在 selenium 脚本在 python 中运行时停止它,然后从头开始重新启动它? - How do I stop a selenium script while it's running in python and then start it again from the begining? 有没有办法让python子进程在脚本运行一次后不终止? - Is there a way to make python subprocess not terminate after running through the script once? 如何从第一个python脚本启动几个python脚本,然后立即终止它们? - How to launch a couple of python scripts from a first python script and then terminate them all at once? 再次运行Python脚本 - Running Python Script Again 用python脚本终止正在运行的应用程序 - To terminate the running application in python script 当许多 python 脚本正在运行时,如何终止一个 python 脚本? - How to terminate one python script when many python scripts are running? 如何从python停止shell脚本(运行无限循环)? - How to stop a shell script (running a infinite loop ) from python? Python:如何从另一个脚本终止一个脚本的功能 - Python: how to terminate a function of a script from another script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM