简体   繁体   English

如何在Windows上检测正在运行的脚本?

[英]How to detect a running script on windows?

I have a simple script running on windows background. 我有一个在Windows背景上运行的简单脚本。 I will start it when something happen.how can i detect is there the script is running? 当事情发生的时候我会启动它。我能检测到脚本是否在运行?

import threading
def sayhello():
    print "hello world"
    global t        #Notice: use global variable!
    t = threading.Timer(5.0, sayhello)
    t.start()

t = threading.Timer(5.0, sayhello)
t.start()

You can use the isAlive method on the thread to check if it is already running. 您可以在线程上使用isAlive方法来检查它是否已在运行。 On similar lines, if you were running a process instead, then you could write a pid file everytime you start the process and query the OS for the status of that specific pid anytime you required. 在类似的行上,如果您正在运行一个进程,那么每次启动进程时都可以编写一个pid文件,并在需要时随时向操作系统查询该特定pid的状态。

Check the following link for more on isAlive. 有关isAlive的更多信息,请查看以下链接。

http://docs.python.org/2/library/threading.html#threading.Thread.isAlive http://docs.python.org/2/library/threading.html#threading.Thread.isAlive

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

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