'Is there any way to run selenium in python with cron job

I've created a python script that download some file using selenium, something like this.

options = webdriver.ChromeOptions()
#file download path 
prefs = {'download.default_directory' : main_directory}
options.add_experimental_option('prefs', prefs)
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--window-size=1920x1080")

driver = webdriver.Chrome(executable_path='chromedriver', options=options)
driver.get(panel_link)

This script is working fine when I execute it on terminal on my ubuntu 18.04.5 Server. But when this script run from crontab it always stop execution when loading chromedriver. I got this error every time, I run this python script via cron.

Message: Service chromedriver unexpectedly exited. Status code was: -5

What I've tried:

 1. add python script directly in crontab 
 2. created other script ```runpython.sh``` and then run python script from there.
 3. created an apache server and tried to execute python script from php 

All above methods were not working correctly. Please Note that this python script is executing perfectly when I execute directly but It is not working when executed via cron.

cron:

48 21 * * * export DISPLAY=:0 && python3 /root/script/application_file_download.py >> /home/log/cron.log 2>&1



Solution 1:[1]

A few things you can try:

  1. for sanity, add a job that writes something to a log file to ensure your cronjob is setup correctly.

    * * * * */1 echo $(date)" cronjob ran" >> /home/<user>/cronlogs/logs.log

  2. Note that paths should be absolute paths. CRON restricts PATH to /bin:/user/bin. If your command is in a different location, you can provide the full path to your command

  3. Check execute permissions on the chromedriver file. Ensure that the user running cronjob has the needed permissions. ls-l

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GAD3R