I want to run the following at 22:00 PM everyday using import datetime. I'm trying to use a codeRan boolean to trigger it, but I can't get it to work. It's always False:
import datetime
timeNow = datetime.datetime.now() # 2022-10-31 10:23:10.461374
timeHour = timeNow.strftime("%H") # 22
timeFull = timeNow.strftime("%X") # 10:21:59
timeAMPM = timeNow.strftime("%p") # AM or PM
codeRan = False
if timeHour == "22" and codeRan == False:
print(timeHour + " That is correct!")
codeRan = True
elif timeHour == "22":
print("Script already ran. Wait 24 hours")
else:
print("Not time yet, it's only " + timeFull + ". The script will run at 22:00" + timeAMPM)
cron(or your system's equivalent) to run programs on a specific schedule.