在这里插入代码片
import os
import time
# tv2.py 是一个需要一直运行的任务线程。
#任务线程每执行一次都往os.path.join('config', 'check.txt')该路径下
#写下当前时间戳,即任务进程的心跳
# 进程杀死程序,防止进程假死影想进程重新启动
def kill_progress(pname):
# 查询py程序的运行进程id
run_status = os.popen('pgrep -a python|grep {}'.format(pname))
pid = run_status.read().split(' ')[0]
# 使用kill杀死进程
cmd = os.system('kill {}'.format(pid))
def run():
#判断时间间隔(单位秒)
sleep_time_thr = 60
try:
# 读取check.txt文件(任务进程的心跳)
with open(os.path.join('config', 'check.txt'), 'r') as timet:
# 任务进程最近一次的心跳时间
last_time = timet.read()
#当前时间
current_time = time.time()
#如果last_time 时间比较新,说明任务线程一直活着,否则就挂了,需要重新启动。
if current_time - float(last_time) < sleep_time_thr:
print('It is running, please wait')
time.sleep(10)
else:
#读取主任物的线程id,并杀死。因为这个时候有可能是假死,所以要先杀死,再重新启动
run_status = os.popen('pgrep -a python|grep tv2.py')
pid = run_status.read().split(' ')[0]
cmd = os.system('kill {}'.format(pid))
#启动任务线程
s = os.system('nohup python tv2.py & > nohupout1.out &')
print('rerun..')
time.sleep(10)
except:
print('RUN error1')
if __name__ == "__main__":
"""
Examples:
run
"""
while True:
run()
print('waiting for new data')
for i in range(10):
print('waiting for new data {} '.format(i))
time.sleep(1)
11-27