1、需求规定:
每天16点的每分钟的第20s定时执行一个.py文件,执行完时再休息3s。
2、代码实现:
(1)Schedule.py
import datetime,os,time
coding='utf-8'
def job(interval,command):
while True:
now=datetime.datetime.now()
if now.hour==16:
os.system(command)
print("检测:",now.hour,":",now.minute,":",now.second)
time.sleep(interval)
if __name__=='__main__':
interval=3
command="python PrintFuc.py"
job(interval,command)
(2)PrintFuc.py
coding='utf-8'
def main():
print("定时任务测试*******")
if __name__=='__main__':
main()
(3)结果:
定时任务测试*******
检测: 16 : 51 : 8
定时任务测试*******
检测: 16 : 51 : 11
......
(4)注意:
command="python PrintFuc.py"处,如果是Python3命令,则用command="python3 PrintFuc.py"