2

I want to run a script at every 5 minutes but for the time when I run it, I want it to wait for divisible 5 minutes. For ex - current time is 11:32:15 am, so for the first time I would like it to run from 11:35 and then after every 5 minutes till 3:15 pm

ashish gupta
  • 43
  • 1
  • 5
  • Have you tried anything yet? What went wrong? – Selcuk Jan 27 '22 at 06:08
  • 2
    Use `cron` to run a command on a schedule. – Barmar Jan 27 '22 at 06:10
  • The generic answer is indeed `cron`, but if you have a long-running Python process in which you want to schedule things, see https://docs.python.org/3/library/sched.html – AKX Jan 27 '22 at 06:18
  • See [this answer](https://stackoverflow.com/a/59203700/12299000) about the `schedule` module. – kaya3 Jan 27 '22 at 06:41
  • The general algorithm is: get the current time, compute the delay till the next run, sleep the computed amout, run your command. For recurring commands put that into a loop. – VPfB Jan 27 '22 at 07:46

1 Answers1

-1

You can use the schedule package, it is flexible for this kind of task.

import schedule
import time

def job():
    print("I'm working...")

schedule.every(5).minute.until("2030-01-01 03:15").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
Andrea Di Iura
  • 467
  • 5
  • 11