2

I have a python script which takes 30 seconds to run and output the data, I require. I want the python script to continually re-run after the python script has finished for a period of 15 minutes. I have tried using Windows Task Scheduler, but it can not be configured to run programs every 30 seconds (minimum is 1 minute). I also want to the python script to re-run after the python script is finished, not in intervals of 30 seconds.

onthepunt
  • 47
  • 5

1 Answers1

1

How about something like this:

nest your script in a while(True):. At the end of your script, put a time.sleep(15*60). So:

def main():
    # do your script 

if __name__=="__main__":
    while(True):
        main()
        time.sleep(15*60) # time in seconds, so 30 if you want 30

With this, just run your script with python script.py. Can also run it in the background if you'd prefer to free up the console.

GLaw1300
  • 195
  • 9
  • Is _name_ mean't to be the name of my python script? – onthepunt Jul 08 '20 at 05:59
  • @onthepunt You can read up on what the \_\_name\_\_ variable is in python here: https://www.tutorialspoint.com/name-a-special-variable-in-python but for simplicity's sake all this is saying is if you run the python script from the command line (not import it as a module), \_\_name\_\_ will be "\_\_main\_\_" which will in turn trigger the loop to run. – GLaw1300 Jul 08 '20 at 06:01
  • @GLaw1300 please include `import time` in your answer. Otherwise, it'll through an error like this `NameError: name 'time' is not defined` – Mrinal Roy Jul 08 '20 at 06:13
  • Ok, thanks. Are you sure time.sleep(15*60) is appropriate for running the program every 30 seconds. – onthepunt Jul 08 '20 at 06:15
  • @onthepunt Oh haha I misread your post, for every 30 seconds it would just be time.sleep(30). I thought you wanted 15 minutes. – GLaw1300 Jul 08 '20 at 06:21
  • @onthepunt your question is confusing. Please elaborate on your question. It takes 30 seconds to run and the script ends, right? You want to wait 15 mins to re-run the script again. That's what the author of this answer understood and suggested above. Differentiate re-run of 15 mins vs 30 seconds. – Mrinal Roy Jul 08 '20 at 06:22