简体   繁体   中英

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. 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. 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.

  • * 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
  • I don't believe there is a jobs function.

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):

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

Then, add it using add_job (not 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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