0

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)
5
  • If you run this code anytime during the 10 o'clock hour, you will get the results you want. Of course, that depends on YOU running the code at the right time, which seems to defeat the whole pupose... Commented Oct 30, 2022 at 21:28
  • 1
    Generally, you would use cron (or your system's equivalent) to run programs on a specific schedule. Commented Oct 30, 2022 at 21:29
  • It is always false because it is currently not 22 Commented Oct 30, 2022 at 21:30
  • Sorry I forgot to mention - I tested it yesterday while it was 22:00 pm - but ideally would want to run it once per day at a specific time & then trigger the boolean to stop it. I guess I can use task scheduler etc to run it, but would prefer everything within python Commented Oct 30, 2022 at 21:35
  • So when you ran it yesterday at 22:00, did it work as expected? Commented Oct 30, 2022 at 21:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate. Given me plenty to think about. I can work with this
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.