简体   繁体   English

通过APSCHEDULER在每次1分钟和31分钟时在python中安排cron作业以运行python脚本

[英]Scheduling a cron job in python to run a python script every time at 1 and 31 minutes through APSCHEDULER

I have to run a AP scheduler cron job every 1st and 31st minute.我必须每 1 分钟和 31 分钟运行一次 AP 调度程序 cron 作业。 For example if it runs at 10.01 and it should run again 10.32,then it should run again 11.03 and 11.34 and it should repeat for all days except saturday and sunday.例如,如果它在 10.01 运行并且应该在 10.32 再次运行,那么它应该在 11.03 和 11.34 再次运行,并且除了周六和周日之外的所有日子都应该重复。 I tried this and it's not working我试过这个,但它不起作用

How should I do it我该怎么做

sched.jobs(jobs,'cron',day_of_week='mon-fri', hour=*, minute=*\31*)

The above is not valid Python, and probably wouldn't run.以上不是有效的 Python,可能无法运行。

  • * without quotes is interpreted as a multiplication operator, but it's in an illegal place. *不带引号被解释为乘法运算符,但它位于非法位置。
  • Also, the cron expression */31 * * * Mon-Fri means to run on minutes divisible by 31. The cron expression you want is 1,31 * * * 1-5此外,cron 表达式*/31 * * * Mon-Fri表示在可被 31 整除的分钟内运行。您想要的 cron 表达式是1,31 * * * 1-5
  • I don't believe there is a jobs function.我不相信有jobs职能。

First you need to define a function to be called (I am not sure if jobs in your expression is a single function or collection of functions, but it should be just one function):首先,您需要定义一个要调用的函数(我不确定表达式中的jobs是单个函数还是函数集合,但它应该只是一个函数):

def job():
    print("job starting")
    call(['touch', 'emptyfile'])

Then, add it using add_job (not jobs )然后,使用add_job (不是jobs )添加它

// create scheduler using a subclass of BaseScheduler
scheduler = BackgroundScheduler()
scheduler.configure(timezone='utc')

// cron would look like 1,31 * * * 1-5
scheduler.add_job(job, 'cron', day_of_week='1-5', hour='*', minute='1,31')
scheduler.start()

You can refer to https://crontab.guru/ and https://apscheduler.readthedocs.io/en/stable/userguide.html可以参考https://crontab.guru/https://apscheduler.readthedocs.io/en/stable/userguide.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM