Last Updated: 24 July 2019
Save yourself hours today on Django projects with my free guide, ?Top Time-Wasting Beginner Django Mistakes? while still available
I found not necessarily conflicting data regarding how to set-up a Python script as a cron job across various sources, but the information was not as clear as it could have been in my opinion.
So this is my quick tutorial..
Update: You can also watch a video of me implementing another Python script as a cron job in not even 10 minutes, https://www.youtube.com/watch?v=kL5rmcxwgSs
My script
from datetime import datetimeprint(datetime.now())
You do not need to change the file permissions of the Python file or include a shebang e.g. #!/usr/bin/python3, unless, you want to set-up your cron job like the last example in this article.
Cron Jobs and Virtual Environments
Python Virtual Environments are a staple of Python development, https://packaging.python.org/guides/installing-using-pip-and-virtualenv/, allowing a developer to isolate versions of Python, library requirements etc. But this introduces the question, ?How do I access the modules of that environment?. After creating the environment, activate it, and execute
which python
The returned path is the Python executable to use in the cron job definition, and the environment-specific modules will be available.
Set-up the Cron Job
If your Python script does not require any root permissions, in the terminal, use the command:
crontab -e
Otherwise:
sudo crontab -e
Using sudo actually gave me a headache and I could not get my basic cron job to execute.
Save yourself hours today on Django projects with my free guide, ?Top Time-Wasting Beginner Django Mistakes? while still available
Update: You can also watch a video of me implementing another Python script as a cron job in not even 10 minutes, https://www.youtube.com/watch?v=kL5rmcxwgSs
File Paths
Once you have the editor and cron-table open, set up the cron job. For the most part, absolute urls should be used (even for the Python executable), or, relative urls can be used but a few additional inline commands will be required:
In my examples, the Python file was located at /home/gavin/python-job.py. The following example uses absolute urls for the script and Python executable
* * * * * /usr/bin/python3 /home/gavin/python-job.py >> ~/cron.log 2>&1
Alternatively, unless you are using a virtual environment as mentioned earlier, the Python 3 executable can be located using command substitution
* * * * * $(which python3) /home/gavin/python-job.py >> ~/cron.log 2>&1
Using relative urls requires changing directories
* * * * * cd /home/gavin && $(which python3) python-job.py >> ~/cron.log 2>&1
Alternative Approach: Executable Python Script
This requires changing your file permissions on the Python script and making it executable, and it will need a shebang e.g. #!/usr/bin/python3 (or whichever path leads to your python executable), at the top of the file.
Allow the Python script to executable
chmod +x <python file>
This will allow you to setup your cron job in the following manner:
* * * * * /home/gavin/python-job.py >> ~/cron.log 2>&1
That?s all for now, help it clears up a few things. Always appreciate feedback and thoughts.
Save yourself hours today on Django projects with my free guide, ?Top Time-Wasting Beginner Django Mistakes? while still available
Update: You can also watch a video of me implementing another Python script as a cron job in not even 10 minutes, https://www.youtube.com/watch?v=kL5rmcxwgSs
Other articles or sources which helped me along the way:
https://stackoverflow.com/questions/43237488/linux-difference-between-sudo-crontab-e-and-just-crontab-e
https://stackoverflow.com/a/8728014/5401933