有没有办法在后台执行恒定的ping并在ping返回"请求超时"两次以上时重新启动应用程序?
我们在无线方面遇到了问题,当连接超时时,它会冻结手持扫描仪上的 telnet 会话。
我目前拥有的是其他帖子的拼凑而成:
import os
import subprocess
subprocess.Popen([r"C:tester.exe"])
hostname = "google.com"
response = os.system("ping -n 1 " + hostname)
if response == 0:
print (hostname, 'is up!')
else:
print (hostname, 'is down!')
os.system("taskkill /im tester.exe")
这和看起来一样好,因为我以前从未编码过......
我以前从未使用过子进程,我的代码完全基于您发布的代码有效的假设。
你可以试试:
import os
import subprocess
import time
def start_subprocess():
subprocess.Popen([r"C:tester.exe"])
hostname = "google.com"
start_subprocess()
while True:
response = os.system("ping -n 1 " + hostname)
if response == 0:
print (hostname, ' is up!')
time.sleep(2)
else:
response = os.system("ping -n 1 " + hostname)
if response == 0:
print(hostname, ' is up AGAIN')
else:
print (hostname, ' is down!')
os.system("taskkill /im tester.exe")
time.sleep(2)
start_subprocess()
这将启动子进程并进入无限循环,每 2 秒 ping 一次响应。 如果响应是否定的,它将检查是否有另一个响应。如果再次没有响应,它将终止进程,等待 2 秒并重新启动tester.exe
。