Search code examples
pythondatetimetimescheduled-tasks

How can i schedule my Code in Python at every 30 minutes increment after a particular time


I have a time - "09:20:45" And I want to schedule my code after every 30 minutes of this time - "09:20:45".

I don't know how can I do this?

schedule.every(30).minutes.do(output_file_exporting)
while True:
    schedule.run_pending()
    time.sleep(1)

where output_file_exporting is my function name.

So I want to run my code after 09:20:45 on the increment of every 30 minutes like -

09:50:45
10:20:45
10:50:45
11:20:45
11:50:45

on these time my code should be run.


Solution

  • You're making this harder than it needs to be. You just do it like you describe it.

    import time
    import datetime
    
    while True:
        now = datetime.datetime.now()
        if now.hour >= 9 and now.minute in (20,50):
            break
        time.sleep(60)
    
    output_file_exporting()
    schedule.every(30).minutes.do(output_file_exporting)
    ...