I'm trying to create a process that can run jobs on a cron schedule of 0/5 8-17 * * 1-5
and here is my test code:
import argparse
from apscheduler.schedulers.background import BackgroundScheduler
import datetime
import time
cmdline_parser = argparse.ArgumentParser(description='Testing')
cmdline_parser.add_argument('--interval', type=int, default=5)
def task():
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print(f'{now}')
if __name__=='__main__':
args = cmdline_parser.parse_args()
sched = BackgroundScheduler(timezone='EST')
sched.start()
minutes_interval = f'0/{args.interval}'
sched.add_job(task, trigger='cron', day_of_week='mon-fri', hour='8-17', minute=minutes_interval)
while True:
time.sleep(30)
But it is not stopping after 5pm. Please help if I'm using the cron arguments incorrectly.
APScheduler is a Python timer task framework based on Quartz.Tasks based on dates, fixed intervals, and crontab types are provided and can be persisted. 1. Install APScheduler
APScheduler has three built-in triggers: date: Use when you want to run the job just once at a certain point of time interval: Use when you want to run the job at fixed intervals of time cron: Use when you want to run the job periodically at certain time (s) of day
Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please. If you store your jobs in a database, they will also survive scheduler restarts and maintain their state.
When the range is in ascending order, the parameter indicates that the results should exclude/include the entire ending hour. When the range is in descending order, the parameter indicates that the results should exclude/include the entire starting hour. 1. Ascending Time Range – Minute Interval – Default
cron hours index from 0 so use hour='7-16' instead of hour='8-17'
sched.add_job(task, trigger='cron', day_of_week='mon-fri', hour='7-16', minute=minutes_interval)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With