简体   繁体   English

从周一至周五每天从 9:30:42 到 15:00:42 每 15 分钟执行一次 python 脚本

[英]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.

相关问题 单个 Cron 表达式在 Spring Boot 中每 5 分钟在 14:30 到 15:30 之间运行计划任务? - Single Cron expression to run a scheduled task between 14:30 to 15:30 for every 5 mins on FRI & SAT in Spring Boot? Crontab-如何使脚本从12:00 PM到03:00 AM每15分钟运行一次 - Crontab - How to make script run every 15 minutes from 12:00 PM to 03:00 AM crontab从午夜后00:30开始每15分钟运行一次 - crontab run every 15 minutes starting at minute 00:30 after midnight 从7:45到17:15每隔30分钟安排一次石英计划 - Scheduling with quartz every 30 minutes from 7:45 to 17:15 java - 如何编写一个在春季/java中工作日从上午9:30到凌晨4:00每5分钟运行一次的cron表达式? - How to write a cron expression that runs every 5 mins from 9:30 to 4:00 am on weekdays in spring/java? 如何将 SingleThreadScheduledExecutor 配置为在上午 8 点到 10 点之间每 15 分钟运行一次作业,然后全天每 1 小时运行一次 - How to configure SingleThreadScheduledExecutor to run a job every 15mins between 8am to 10am and then every 1hr throughout the day 使用Quartz Scheduler在每天4:00 pm和6:15 pm进行cron工作? - a cron job using Quartz scheduler at 4:00 pm and 6:15 pm every day? Spring Cron 表达式在特定时间每 15 分钟运行一次,并且在几天内不运行 - Spring Cron expression every 15 mins for particular hours and not running on days 在特定时间和每天每15分钟运行一次Cron作业 - Run Cron Job Every 15 Minutes in specific time and every day 编写一个cron表达式,每15分钟执行一次 - Writing a cron expression to execute every 15 minutes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM