It is possible to achieve the result using the schedule
module.
First calculate the time until the 5 minute mark. Run a one off job that starts at that time, and use that job to trigger a job that runs every 5 minutes.
import schedule
import time
from datetime import datetime, timedelta
print("Started Exec:- " + str(datetime.now()))
def repeat_job():
print("repeat_job:- " + str(datetime.now()))
def initial_job():
print("initial_job:- " + str(datetime.now()))
schedule.every(5).minutes.do(repeat_job)
return schedule.CancelJob
# Round the current time to the next 5 minute mark.
tm = datetime.now()
tm -= timedelta(minutes=tm.minute % 5,
seconds=tm.second,
microseconds=tm.microsecond)
tm += timedelta(minutes=5)
schedule.every().day.at(tm.strftime("%H:%M")).do(initial_job)
while True:
schedule.run_pending()
time.sleep(1)
This gives roughly the desired result, although the time appears to drift beyond the 5 minute mark by a couple of seconds.
Started Exec:- 2022-09-08 16:13:13.010702
initial_job:- 2022-09-08 16:15:00.181704
repeat_job:- 2022-09-08 16:20:00.638851
repeat_job:- 2022-09-08 16:25:01.066414
repeat_job:- 2022-09-08 16:30:01.492325
repeat_job:- 2022-09-08 16:35:01.899328
repeat_job:- 2022-09-08 16:40:02.353182
repeat_job:- 2022-09-08 16:45:02.785273