Run script at specific time with Python

Question:

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)
Asked By: Spykerwolf

||

Answers:

I don’t know the requirements of your script, but if you simply want to solve the proposed problem, a simple while is missing:

while not codeRan:
    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)

To prevent checking without a minimum wait, you can insert a sleep at the end of each iteration:

while not codeRan:
    if timeHour == "25" 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)

    time.sleep(num_of_seconds)
Answered By: Giuseppe La Gualano

The code as you wrote it will run the code print(timeHour + " That is correct!") and print 22 That is correct! if your script is run between 10pm-11pm. However, you have not included a loop in your code, so you would need to use some external method to call the script frequently to check whether it is time to run your other script. In linux, you could use crontab to schedule this to run every five minutes, but then if you were planning to do that you could just use crontab to schedule it to run at 10 pm. In windows, TaskScheduler can also be used.

If you want to use python, you really have three options which I can think of (realistically probably way more).

  1. use a while loop (probably the easiest yet least elegant solution because the code would run 24/7
while True:
   {your example code here}
  1. Use cron or TaskScheduler

  2. Use subprocess.call to do #2 with Python. Could even check sys.platform to determine whether you are on Linux or Windows.

  3. Check the time, then sleep until it is time to run the code. Again, not great because it means the process must stay alive this entire time.

Additionally, while your method or comparing timeNow.strftime("%H") == '22' does work you also could use time.hour == 22.

Answered By: Modularizer
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.