Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set hour range and minute interval using APScheduler

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.

like image 564
BugCatcherJoe Avatar asked Mar 27 '20 19:03

BugCatcherJoe


People also ask

What is apscheduler?

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

What triggers are available in 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

What is advanced Python scheduler (apscheduler)?

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.

What is the difference between ascending and descending time range?

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


1 Answers

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)
like image 152
Dalton Pearson Avatar answered Oct 10 '22 06:10

Dalton Pearson