[英]How to schedule a python function to run every hour at specific minute (ex 00, 10, 20) with apscheduler
I try to schedule a function within my script to run at a specific minute every hour, ie if my script starts running at 11.46 my function to run at 11.50, 12.00, 12.10 etc and to do the same thing if it fails and restarts. 我尝试将脚本中的函数安排为每小时在特定时间运行,即,如果脚本以11.46开始运行,则函数以11.50、12.00、12.10等运行,如果失败并重新启动,则执行相同的操作。 I have seen that it is possible to do this with cron from command line but is there a way to do it while the script is running with a scheduler?
我已经看到可以从命令行使用cron做到这一点,但是当脚本使用调度程序运行时,有没有办法做到这一点?
I have already created a scheduler which runs every 10 minutes after my script starts, but I want my function to run on specific minutes not just specific intervals. 我已经创建了一个调度程序,该脚本在脚本启动后每10分钟运行一次,但是我希望函数在特定的分钟上运行,而不仅仅是特定的间隔。
from apscheduler.schedulers.background import BackgroundScheduler
import atexit
def demo_job:
print("test")
try:
sched = BackgroundScheduler(daemon=True)
sched.add_job(demo_job, trigger='interval', minutes=10)
sched.start()
atexit.register(lambda: sched.shutdown(wait=False))
except:
print("Unexpected error:")
If my script starts running at 11.47 the first scheduled call of my function is at 11.57, the next at 12.07, 12.17 etc... I want the first run to be at 12.00, the next at 12.10, 12.20 etc 如果我的脚本在11.47开始运行,则我函数的第一个计划调用在11.57,下一次在12.07、12.17等...我希望第一次在12.00、12.10、12.20等运行。
You can try using the cron trigger . 您可以尝试使用cron触发器 。
sched.add_job(
demo_job,
trigger='cron',
minute='*/10',
hour='*'
)
The expression */10
will fire the job at every tenth minute, starting from the minimum value. 从最小值开始,表达式
*/10
将每10分钟触发一次作业。 Crontab ref Crontab参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.