0

This question already has an answer here:

I am running Ubuntu 18.04 LTS on an Amazon LightSail instance.

When I try to run a Python script as soon as I log, it runs perfectly. I run my script with the full path in with the command:

/usr/bin/python3.7 /home/ubuntu/FolderContainingPythonScript/PythonScript.py

The script runs perfectly without issue.

However, when I run the exact same command through a cronfile, the script does not even run at all:

0 5 * * * /usr/bin/python3.7 /home/ubuntu/FolderContainingPythonScript/PythonScript.py

I realized that my script was not even getting past the import statements in it because the variable PATH was not defined in my crontab. To fix this, I then ran my crontab with path specified:

PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
0 5 * * * /usr/bin/python3.7 /home/ubuntu/FolderContainingPythonScript/PythonScript.py

This time my script did get past the import statements and managed to generate a log file. It still does not run properly though. Selenium fails to initialize in it. My guess is beacause selenium requires some path/enviroment variables I have not yet set in my Crontab. I'm not entirely sure that is why my script is failing to run selenium, but that is my best guess, the environment variables.

How can I get cron to correctly run my Python Script with selenium? My hunch is setting the shell to bash, but even if I do that with a shebang line, cron does not run the script properly.

!#/bin/bash
PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
0 5 * * * /usr/bin/python3.7 /home/ubuntu/FolderContainingPythonScript/PythonScript.py

I have also tried this combination:

SHELL=/bin/bash
PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
0 5 * * * /usr/bin/python3.7 /home/ubuntu/FolderContainingPythonScript/PythonScript.py

Can anyone tell me how to set my Cron environment to be exactly that of my regular environment? If that is not what I need, can someone show me the best way to get my Python script running with selenium? Do I have the incorrect she-bang line? Is there a certain Path variable I need to specify? Again, everything runs perfectly fine through the command line of the shell, its just Cron that is giving me errors.

marked as duplicate by Jeff Schaller, Rui F Ribeiro linux Feb 28 at 17:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Are you trying to run it as root? I bet the script / selenium / firefox do not like it. – Rui F Ribeiro Feb 28 at 17:28
  • Load variables for the cron command with a specific file 5 1 * * * .profile ; command for example – francois P Feb 28 at 17:29
  • Install the command in your_ user's_ cron table; not root's. Also, if your imports are looking for modules in the CWD, prefix your cron command: 5 1 * * * cd ~myuser; /path/to/python3 /path/to/script/py. – DopeGhoti Feb 28 at 17:30
  • The issue was running "sudo crontab -e". Like Rui F Ribeiro said I should be on "crontab -e". Well, there goes 10 hours of my life. Thanks for the help. (This may be marked as a duplicate question but the answers I got where not duplicates.) Thank you all for awnsering, problem was solved in like 5 minutes. – Random Programmer Feb 28 at 18:13