0

Im attempting to perform an if statement at a certain time but can't get it to execute when it is that time. I am running the program when it reaches that time and nothing happens.

import os
import datetime

now = datetime.datetime.now()

if now.hour == 20 and now.minute == 46:
    print ("REBOOTING")
    os.system("shutdown -t 0 -r -f")
  • You probably need to use a `while` loop or it will reboot only if by chance you call the program just at the right time – Julien Sep 13 '17 at 02:53

2 Answers2

0
import schedule
import time
def reboot():
    print ("REBOOTING")
    os.system("shutdown -t 0 -r -f")
schedule.every().day.at("20:46").do(reboot)
while True:
    schedule.run_pending()
    time.sleep(1)
Brandon Kheang
  • 597
  • 1
  • 6
  • 19
  • Looks like you copied the majority of your answer from [this answer](https://stackoverflow.com/a/16786600/3496038) from [the question linked as a possible duplicate in the comments](https://stackoverflow.com/posts/comments/79338795?noredirect=1). You didn't event write your own explanation, or include an explanation at all, for that matter.. – Zach Gates Sep 13 '17 at 03:03
  • 1
    I learned in from a site. You can find the information for schedule here: https://pypi.python.org/pypi/schedule – Brandon Kheang Sep 13 '17 at 03:07
-2

for this to happen your code should be in a while loop so:

import os
import datetime
while true
 now = datetime.datetime.now()

 if now.hour == 20 and now.minute == 46:
    print ("REBOOTING")
    os.system("shutdown -t 0 -r -f")
Daniyal Ahmed
  • 715
  • 5
  • 11