-1

I have a python script that search for something in the internet.the script takes 2 minutes to finish. after 2 minutes script will exit and the job is done.

but i want to run this script on my computer constantly for a day without stopping. it means i want to rerun the script after it's finished for a day.

i tried something like this so far:

status = True
def setstatus(stat):
    global status
    if staus:
       status = not stat
timer = Timer(1440,setstatus,[True])
timer.start()

while status:
   # do the job

any suggestion?

  • ``status = not status`` will only set the *local* variable ``status``. It will not set the *global* variable checked in ``while status``. – MisterMiyagi Sep 09 '20 at 11:30
  • 2
    Wrap the whole thing in another while loop which checks for the time now being 24hrs from when it started? – quamrana Sep 09 '20 at 11:30
  • if you're on Linux you can create a cron job – PYPL Sep 09 '20 at 11:34
  • If you are on Windows, then see [Setting up a cron job in Windows](https://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows#:~:text=The%20windows%20equivalent%20to%20a,add%20it%20to%20version%20control).) – Booboo Sep 09 '20 at 11:41

2 Answers2

0

time.time() will give you the current time (in seconds from some time origin, in fact at the start of 1970). Calculate the end time by adding a number of seconds from the current time, and loop until then.

import time

end_time = time.time() + 86400

while time.time() < end_time:
    # do the job

Obviously this will keep using as much CPU as your job requires -- your stated aim is "to run this script on my computer constantly for a day without stopping" and this will do what you have asked for. If in fact you want it to reduce CPU consumption by sleeping for a while on each iteration, then you can insert a time.sleep(...) in the loop (with sleep time value in seconds).

alani
  • 12,573
  • 2
  • 13
  • 23
-1

Only works for GNU/Unix based systems.

You can use Cron.

For exemple, running a script everyday at 10 a.m.

Enter crontab -e in terminal. Add 0 10 * * * /path/to/script.py to the prompted file.

If you are using it with a web-scraper as your question states, you can have a shell script to encapsulate your python script.

#!/bin/bash

# setup variables
DATE=$(date +"%Y-%m-%d-%H-%M")
SCRIPT_FILE="path/to/python/script.py"
OUTPUT_FILE="path/to/save/outputs/${DATE}.json"

# activate virtualenv and run scrapy, for example
source path/to/venv/bin/activate
scrapy runspider ${SCRIPT_FILE} -o ${OUTPUT_FILE}
mlisthenewcool
  • 539
  • 3
  • 14
  • I'm familiar with cron . as you said it runs everyday at 10. but i want it to work for day – omid mirakhorli Sep 09 '20 at 11:55
  • I don't understand what you're asking for. Running a script exactly for one day long and stop it then ? You need the same script to restart consistently during one day or the python script will be dedicated to that ? Can you precise what you are looking for please ? – mlisthenewcool Sep 09 '20 at 13:45