python定时任务调度库apscheduler的使用

时间:2022-05-29 07:46:22
import  os
import time
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def tick():
file = open('log.txt', 'a+')
file.write(str(datetime.now())+'\n')
file.close()
print 'Tick! The time is: %s' % datetime.now()

if __name__ == '__main__':
scheduler = BlockingScheduler()
#scheduler.add_job(tick, 'cron', second='*/10', hour='*')
scheduler.add_job(tick, #程序的运行时间截止到2017-11-12,运行的时间点是周一-周五的凌晨五点半
'cron',
day_of_week='mon-fri',
hour=5,
minute=30,
end_date='2017-11-12')
print 'Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C')
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()

在后台查看运行的进程可以使用:

pstree |grep python 

或者

ps -ef |grep python 
ps aux  #查看系统的所有进程

可以看到:
python定时任务调度库apscheduler的使用

具体的apscheduler使用方法