# 设置每8小时运行一次
def setTimer(sched_timer):
flag =0
while True:
now =str(datetime.datetime.now())[:-7] # 获取当前时间
if now ==str(sched_timer):
print('Run at'+str(sched_timer))
function() # 要运行的程序
time.sleep(1)
flag =1
else:
if flag ==1:
sched_timer=sched_timer+datetime.timedelta(hours=8) # 8小时后再执行
flag=0
if__name__ =='__main__':
sched_timer = datetime.datetime(2017,12,10,22,58,00) # 开始运行程序时间
setTimer(sched_timer)
hours可以换成days,minutes或者seconds