Search code examples
pythonschedule

Schedule a Job every minute on the exact minute during specific times using Python Schedule Library?


I am using the Python Schedule Library and I have been using the following line of code to schedule a job to run every minute, on the exact minute regardless what time the program is started. For instance, if the program is ran at 13:51:30, rather than starting one minute after that time which would be 13:52:30, it will start at 13:52:00. This is the line of code used to achieve this:

schedule.every(1).minutes.at(":00").do(job)

Now, how can I get schedule to do this between specific times? For instance, if I want this schedule to occur during 10:00 till 11:00?


Solution

  • I hope I understood the question correctly. I use this:

    def func():
    now_datetime = datetime.now()
    if (now_datetime.hour >= 10) & (now_datetime.hour < 11) :
        print(now_datetime)
    
    
    def main():
        while True:
            schedule.every().minute.at(':00').do(func)
    
            while True:
                schedule.run_pending()
                time.sleep(1)