我正在使用python监视一个特定的PID并尝试执行一个函数,如果这个PID不再存在则再次启动它。问题是我的循环似乎适用于1,5(是,一个半)循环,然后自行打破。
while True:
print "[DEBUG] We are in the loop"
query = "Select * from Win32_Process where ProcessId = " + str(monitorPID)
if (GetObject('winmgmts:').ExecQuery(query).count == 0):
RunTheProgramAgain()
print "[DEBUG] Current PID is %d - ProcNAME is %s" % (monitorPID, procName)
time.sleep(5)
它将成功重新运行程序,它也会更新程序的PID,但是当第二个循环发生时,它会在最后一次调试打印之前无故断开。任何帮助将不胜感激。
答案 0 :(得分:1)
尝试在try块中包装代码,看看是否有任何异常被抛出。
while True: try: print "[DEBUG] We are in the loop" if (GetObject('winmgmts:').ExecQuery("Select * from Win32_Process where ProcessId = " + str(monitorPID)).count == 0): RunTheProgramAgain() print "[DEBUG] Current PID is %d - ProcNAME is %s" % (monitorPID, procName) time.sleep(5) except Exception as e: print e pass