customer relationships

Using Crontab and Selenium

I have been setting up a python script using Selenium to automate some web tasks. One of the tasks I wanted to automate completely. I ran into a few problems trying to run the python script in crontab. The python script would run fine in the terminal but not using crontab. After a lot of Googling and StackOverFlow research I came up with a solution to the problem.

There are two issues:

  1. Crontab uses very limited PATH variables.
  2. Crontab doesn’t use a display or terminal to run code. Everything runs in the background.

Initially, I was getting this error: “Message: ‘geckodriver’ executable needs to be in PATH.”

The fix for that is to add the PATH to the browser driver to the cronjob.

The second issue I ran into was the cronjob not having a display to use.

The fix for that is to add a Display to the cronjob command.

I set this up in Ubuntu 19.04 using crontab -e

Here is the end result:

* * * * * export DISPLAY=:0 && export PATH=$PATH:/usr/local/bin && /usr/bin/python3 /home/username/Documents/Project/project.py

This cron job will run every minute.


Got a project that needs expert IT support?

From Linux and Microsoft Server to VMware, networking, and more, our team at CR Tech is here to help.

Get personalized support today and ensure your systems are running at peak performance or make sure that your project turns out to be a successful one!

CONTACT US NOW
one click social media designs