简体   繁体   English

在Windows中检查Python程序是否正在运行

[英]Check if Python program is running, in Windows

I am in the process of creating a Python program (lets call it Program1), which will terminate randomly. 我正在创建一个Python程序(将其称为Program1),该程序将随机终止。 I need another program to check if Program1 has been terminated, and if so, to restart it after a given amount of time. 我需要另一个程序来检查Program1是否已终止,如果已终止,请在给定的时间后重新启动它。 Should I use another Python program to do this, or is there a way this can be done in Windows eg similar to a cron job? 我应该使用另一个Python程序来执行此操作,还是可以在Windows中完成类似于cron作业的方法?

From what I understand, you can use sys.exit(), exit() and quit() to kill a Python program, but do all of these also kill all other running Python programs? 据我了解,您可以使用sys.exit(),exit()和quit()杀死一个Python程序,但是所有这些还杀死了所有其他正在运行的Python程序吗?

Thanks 谢谢

Reliably determining whether a specific program is still running or not can be done in multiple ways. 可以多种方式可靠地确定特定程序是否仍在运行。

A good way to do so would be using a specific lock file. 这样做的一个好方法是使用特定的锁定文件。 You can find how to do this in Python described in this thread . 您可以在该线程中找到如何在Python 中执行此操作

To answer the second part of your question: sys.exit() , exit() and others will only stop the currently running Python process, not any others that might be running concurrently. 要回答问题的第二部分: sys.exit()exit()和其他命令只会停止当前正在运行的Python进程,而不会停止可能同时运行的其他任何进程。

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

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