简体   繁体   中英

how to run a python script constantly for a day

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?

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).

Only works for GNU/Unix based systems.

You can use Cron .

For exemple, running a script everyday at 10 am

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM