0

I currently have a big long file that queries a webpage to build a dictionary. I'd like this file to restart at 4am every day as the webpage will have updated with fresh info. What do I need to put inside my while True: loop?

Current status:

##Various Imports
##Selenium code to get details
##Dictionary Compile

while True:
    now = datetime.datetime.now()
    current_time = now.strftime("%H:%M:%S")
    if current_time == ("04:00:00"):
        ##The Code to Restart the process goes here
    else:
        #Other Stuff happens with the dictionary

Building and testing on windows but will ultimately run on a Raspberry Pi

2 Answers2

1

I would suggest you make the script simply get the information from the web page then do whatever it needs to do with your dictionary and end. Only one time. Then you can schedule this script to run at 4:00 AM every day with Windows Task Scheduler or with a Cron Job on linux. Here is a link on how to set up a cron job to run a python script.

Edwin Cruz
  • 507
  • 3
  • 10
  • Ah I never thought of it like that! So I could cron to kill the file at 3:59 and then restart it at 4am! Thanks! – Adam Davies Jun 25 '20 at 15:09
  • Not really. You would only need to run it once to get the data is what I am saying? Or did you need that infinite loop for something else? – Edwin Cruz Jun 25 '20 at 15:10
  • I need the infinite loop for other things. The script is controlling smart lights for the day based on appointments so the infinite loop checks the time and turns on/off whatever it needs to, with a 4am refresh of all bookings – Adam Davies Jun 25 '20 at 15:12
  • So basically at 4:00am you want to get the bookings. Then throughout the day update the lights based on the list of bookings and maybe some other stuff but you require the program to run continuously correct? – Edwin Cruz Jun 25 '20 at 15:23
  • Yes! So a 3:59 termination and a 4am restart would be perfect. Had my head buried so deep in the python I forgot about the existence of cron – Adam Davies Jun 25 '20 at 15:24
  • You could also simply run 2 scripts, one that is a cron job that runs at 4:00am and gets the data and writes it to a file and then another that will run continuously and simply read that file. Which might be a better approach. – Edwin Cruz Jun 25 '20 at 15:33
0

If you want the functionality of a cron job in Python you could use the schedule library. It looks like it has support to keep the script running and then restart at 4AM. More can be read on this StackOverflow Question.

griffin_cosgrove
  • 419
  • 8
  • 16