from datetime import datetime from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.blocking import BlockingScheduler import subprocess,sys import os import datetime IS_WIN32 = 'win32' in str(sys.platform).lower() def alarm(type): print("starting test") Bat() print("end test") def Bat(): cmd = 'sam.bat'#脚本 命令 if IS_WIN32: startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE p = subprocess.Popen(cmd, startupinfo = startupinfo) curline = p.stdout.readline()#命令执行返回输出 while(curline != b''): strLine = str(curline, encoding="utf-8") print("%s"%(strLine)) curline = p.stdout.readline() p.wait() print("%s -- end..... %s "%(datetime.datetime.now().time(), p.returncode)) # 循环执行 # Schedule job_function to be called every 1 hours def interval_trigger(): print("%s -- after wait %s to start.."%(datetime.datetime.now().time(), "2.10 hour")) global SCHEDULER SCHEDULER.add_job(func=alarm, args=['interval'], trigger='interval', hours=2, minutes=10, id='interval_job') SCHEDULER=BlockingScheduler(timezone='Asia/Shanghai') ## 后台 ##SCHEDULER = BackgroundScheduler(timezone='Asia/Shanghai') if __name__ == '__main__': interval_trigger() try: SCHEDULER.start() except (KeyboardInterrupt, SystemExit): SCHEDULER.shutdown()