import schedule
import time
def your_task():
# Your task
print("Task executed")
schedule.every().day.at("14:00:00").do(your_task)
while True:
schedule.run_pending()
time.sleep(1)
This will run your task every day at 14:00:00, regardless of whether the program has been sleeping or not.
Edit:
import time
import json
def your_task():
# Your task
print("Task executed")
def run_pending_tasks():
current_time = time.localtime()
global last_execution_time
last_execution_time_converted = time.strptime(last_execution_time, '%Y-%m-%d %H:%M:%S')
current_date = time.strftime("%Y-%m-%d", current_time)
if current_date in execution_data:
if execution_data[current_date] == 1:
return
execution_data[current_date] += 1
else:
execution_data[current_date] = 1
if (current_time.tm_hour >= 14 and current_time.tm_min >= 0 and current_time.tm_sec >= 0) and (time.mktime(current_time) - time.mktime(last_execution_time_converted)) >= 86400: # 86400 seconds in a day
your_task()
last_execution_time = time.strftime('%Y-%m-%d %H:%M:%S', current_time)
with open('execution_data.json', 'w') as file:
json.dump(execution_data, file)
last_execution_time = '2000-01-01 00:00:00'
try:
with open('execution_data.json', 'r') as file:
execution_data = json.load(file)
except FileNotFoundError:
execution_data = {}
while True:
run_pending_tasks()
time.sleep(300)