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
CodePudding user response:
Use time.sleep run code after 5 min and datetime.datetime to get time
This is code:
from datetime import datetime
import time
while True:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
if current_time=="11:35:15":
# YOUR CODE HERE
time.sleep(300)
elif current_time=="3:15:06":
break
If you want to run 1 times before break you can increase current_time
CodePudding user response:
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)
CodePudding user response:
you can schedule the script using "task scheduler" in windows or "crontab" in linux.
