使用Python的apscheduler模块定时启动程序

前提:感觉windows计划任务感觉并不是那么好用。

需求:想监控一个Python程序是否正常运行。

工具:python的apscheduler模块和os执行本地程序

方法:使用apscheduler模块定时检测/启动程序。

import os
from apscheduler.schedulers.blocking import BlockingScheduler

def start_kp():
  os.system("python D:\\1.py")

sched = BlockingScheduler()

# 每1分钟触发
sched.add_job(start_kp, 'interval', minutes=1,start_date='2010-10-10 09:30:00', end_date='2014-06-15 11:00:00')

sched.start()

Notice:如果Python没有在环境变量。需要用全路径,需要执行的文件,最好使用全路径。

apscheduler的interval使用方法(django中可以使用装饰器模块):https://apscheduler.readthedocs.io/en/latest/modules/triggers/interval.html?highlight=interval#examples

You May Also Like

About the Author: admin

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注