2

I'm trying to make a program that does something every hour, then runs itself, and then kill itself.

The problem I'm having is that the program does not kill itself completely. I see that the process is not gone when I use System Monitor.

Overtime I just get more and more python2 processes that take up ram.

I am using Python 2.7.12 on a 64 bit machine running Arch Linux

This is the code I'm running

def GoToWebsite(username, password):
    chrome_options = webdriver.ChromeOptions()
    prefs = {"profile.default_content_setting_values.notifications": 2}
    chrome_options.add_experimental_option("prefs", prefs)
    chromeBrowser = webdriver.Chrome('/home/daniel/Dropbox/Code/BrowserDrivers/chromedriver',
                                     chrome_options=chrome_options)
    chromeBrowser.get('http://www.website.com')
    while True:
        try:
            picX, picY = pyautogui.locateCenterOnScreen(currentPythonDirectory + '/picture.png')
            break
        except:
            pass
    pyautogui.click(picX, picY)
    time.sleep(3)
    url = chromeBrowser.command_executor._url
    session_id = chromeBrowser.session_id 
    return url, session_id

websiteUrl, websiteSessionId = GoToWebsite("username", "password")
#Do Stuff
originalStartTime = time.time()
currentPythonDirectory = os.path.dirname(os.path.realpath(__file__))

while True:
    if (time.time() - originalStartTime) >= 3:  # 3600:
        chromeDriver = webdriver.Remote(command_executor=websiteUrl, desired_capabilities={})
        chromeDriver.session_id = websiteSessionId
        chromeDriver.quit()
        try:
            chromeDriver.close()
        except:
            pass
        os.system("python2 " + currentPythonDirectory + "/PythonScript.py")
        time.sleep(1)
        sys.exit(1)
        break
    #Other Stuff
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • read that please, http://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used – Ari Gold Oct 24 '16 at 09:54
  • I'm surprised this hasn't been asked, but why? What benefit do you gain from killing/respawning your program? What prevents you from just making a daemon? What are you trying to accomplish by respawning the process? – TemporalWolf Oct 24 '16 at 10:06

2 Answers2

2

I had exactly the same issue when trying to make a better version of crontabs. I modified my code so you understand the approach. With this method you will not run into any max recursion problems.

import os, commands, regex, subprocess
from subprocess import call

allActivePythonProcesses = os.popen('pgrep -lf python').read()
thisIsYourPythonFileProcess = find('\d{7} python myRepeatingFile.py', allActivePythonProcesses )
if thisIsYourPythonFileProcess:
    # Store the Process ID
    convPID = find('\d{7}', thisIsYourPythonFileProcess)
    print "Your Python File is running at PID: " + convPID
else:
    print "Process Controller: Your Python file is not running"
    try:
        print "...Calling your Python file"
        subprocess.check_call('python myRepeatingFile.py', shell=True)

    except subprocess.CalledProcessError as e:
        print "Process Call Error :" + e

if you want it to execute 24/7 just put it into a while True loop. import time module if you want to limit the speed.

Roy Holzem
  • 860
  • 13
  • 25
0

As far as I am aware a subprocess will be launched and once it has completed return. This is a blocking action as python will wait for the subprocess you launched to be completed before executing any other code. Adding a print statement after the os.system() will show that the program never reaches the sys.exit(1)