How to Schedule a Python Script Cron Job

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

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:

Otherwise:

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

Alternatively, unless you are using a virtual environment as mentioned earlier, the Python 3 executable can be located using command substitution

Using relative urls requires changing directories

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

This will allow you to setup your cron job in the following manner:

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

I'm a coder and freelancer from South Africa with 5+ years experience in automations, integrations and API's using Python and Django.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store