Henk Pleister
Henk Pleister

Reputation: 3

is it possible to codein that after every 2 hours this script restarts?

I currently have this script

import os
import time


os.system("killall -9 chromium-browser");


from config import *
with open(PROXIES_FILE) as f: proxies=f.read().split('\n')
proxies=[proxy.strip() for proxy in proxies if proxy.strip()]
for i in range(NUM):
    if len(proxies)<=i: break
    os.system('nohup chromium-browser --proxy-server="http://{proxy}" --disable-popup-blocking -new-window --user-data-dir=~/tmp/f{i} {url}&'.format(url=URL, i=i, proxy=proxies[i]))
    time.sleep(5)
print('%s browsers opened successfully!'%(i+1))

is it possible to script in that it will restart itself after every 2 hours?

Upvotes: 0

Views: 60

Answers (2)

Ludovic Viaud
Ludovic Viaud

Reputation: 202

if you are under linux just use cron

"Cron is a system daemon used to execute desired tasks (in the background) at designated times. "

it's by far the most common tool for this

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

Put the whole code in a function and call that function every 2 hours, and run this script in background by converting it into a daemon. Also to make sure the function executes after almost exactly 2 hours you need to return the total number of seconds the function went to sleep(to get the total number of seconds spent in the function you can add the line t = time.time() at the start of the function and then in the end return time.time() - t).

import os
import time
from config import *

def func():

    os.system("killall -9 chromium-browser");
    with open(PROXIES_FILE) as f: proxies=f.read().split('\n')
    proxies=[proxy.strip() for proxy in proxies if proxy.strip()]
    seconds_slept = 0
    for i in range(NUM):
        if len(proxies)<=i: break
        os.system('nohup chromium-browser --proxy-server="http://{proxy}" --disable-popup-blocking -new-window --user-data-dir=~/tmp/f{i} {url}&'.format(url=URL, i=i, proxy=proxies[i]))
        time.sleep(5)
        seconds_slept += 5
    print('%s browsers opened successfully!'%(i+1))
    return seconds_slept

while True:
    time.sleep(2*60*60 - func())

Now you can run this script as daemon using the solutions from this answer.

Upvotes: 0

Related Questions