5

I've created a web scraper with python (3.6) and a selenium, firefox web driver. I've set up a cronjob to run this scraper every few minutes, and it seems to all be working, except that over time (like a few days), the memory on my Ubuntu VPS (8GB RAM, Ubuntu 18.04.4) fills up and it crashes.

When I check HTOP, I can see lots (as in, hundreds) of firefox processes like "/usr/lib/firefox -marionette" and "/usr/lib/firefox -contentproc", all taking up about 3 or 4mb of memory each.

I've put a

browser.stop_client() browser.close() browser.quit()

In every function that uses the web driver, but I suspect the script is sometimes leaving web drivers open when it hits an error, and not closing them properly, and these firefox processes just accumulate until my system crashes.

I'm working on finding the root cause of this, but in the meantime, is there a quick way I can kill/clean up all these processes?

e.g. a cronjob that kills all matching processes (older than 10 minutes)?

Thanks.

3
  • try creating a bash file with kill geckodriver and call the bash command as part of your cronjob. That should kill all driver instances. Commented May 6, 2020 at 4:21
  • 3
    killall firefox; killall geckodriver Commented May 6, 2020 at 6:03
  • 1
    Thanks guys - this is a great short term solution Commented May 7, 2020 at 5:44

1 Answer 1

4

I suspect the script is sometimes leaving web drivers open when it hits an error, and not closing them properly

This is most likely the issue. We fix this issue by using try except finally blocks.

browser = webdriver.Firefox() try: # Your code except Exception as e: # Log or print error finally: browser.close() browser.quit() 

And if you still face the same issue, you can force kill the driver as per this answer, or this answer for Ubuntu.

import os os.system("taskkill /im geckodriver.exe /f") 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your effort. But, OPs question is specific to ubuntu and AFAIK taskkill does not work on ubuntu. Check my comment on the original question.
Thanks for that, I'll try to incorporate this try/except/finally structure, and check those links out.
Good answer, but there's no need to call browser.close(). browser.quit() already closes all browser windows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.