[英]Execute python script every 15 mins starting from 9:30:42 till 15:00:42 every day from Mon-Fri
I need a way to execute my python script every 15 mins starting from 9:30:42 till 15:00:42 every day from Mon-Fri.我需要一种方法来执行我的 python 脚本,从周一到周五每天从 9:30:42 到 15:00:42 开始,每 15 分钟一次。
I have explored APScheduler with cron syntax but can't figure out how to code the above condition.我已经使用 cron 语法探索了 APScheduler,但无法弄清楚如何对上述条件进行编码。 I tried below but doesn't work (execute is my function name)我在下面尝试但不起作用(执行的是我的 function 名称)
sched.add_cron_job(execute, day_of_week='mon-fri', hour='9:30:42-15:00:42', minute='*/15')
Any pointer is deeply appreciated.任何指针都深表感谢。
As far as I can tell you won't be able to do what you want with a single job.据我所知,你不能用一份工作做你想做的事。
This is the closest I could get with one: 30-59/15 9-14 * * 1-5
which equates to Every 15 minutes, minutes 30 through 59 past the hour, between 09:00 AM and 02:59 PM, Monday through Friday .这是我能得到的最接近的值: 30-59/15 9-14 * * 1-5
相当于每 15 分钟,从 09:00 AM 到 02:59 PM,星期一通过星期五。
Although it isn't exactly what you wanted I hope this helps as a base .虽然这不是您想要的,但我希望这有助于作为基础。
I wrote custom code to solve my problem.我编写了自定义代码来解决我的问题。 Posting here in case it helps someone.在这里发帖以防它对某人有帮助。 Any optimisations suggestions are welcome.欢迎任何优化建议。
The first infinite loop starts the job when the start time is hit.第一个无限循环在启动时间到达时启动作业。 The 2nd infinite loop wakes up every x minutes to check if next run time has approached.第二个无限循环每 x 分钟唤醒一次,以检查是否已接近下一个运行时间。 If yes, it executes else goes back to sleep.如果是,则执行 else 回到睡眠状态。 If the end time for execution has reached, then it breaks out如果已经到了执行的结束时间,那么它就会爆发
def execute_schedule_custom():
start_time_of_day = datetime.combine(date.today(), time(9, 30, 42))
next_run_time = start_time_of_day
end_time_of_day = datetime.combine(date.today(), time(15, 0, 42))
interval = 15
sleep_secs = 60 * 5 #sleep for 5 mins
while True:
if datetime.now() >= start_time_of_day:
execute()
next_run_time = start_time_of_day + timedelta(minutes=interval)
break
while True:
if datetime.now() >= end_time_of_day:
break
elif datetime.now() >= next_run_time:
execute()
next_run_time = next_run_time + timedelta(minutes=interval)
t.sleep(sleep_secs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.