How to Schedule a Python Script Cron Job

My script

from datetime import datetimeprint(

Cron Jobs and Virtual Environments

Python Virtual Environments are a staple of Python development,, 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

Set-up the Cron Job

If your Python script does not require any root permissions, in the terminal, use the command:

crontab -e 
sudo crontab -e 

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:

* * * * * /usr/bin/python3 /home/gavin/ >> ~/cron.log 2>&1
* * * * * $(which python3) /home/gavin/ >> ~/cron.log 2>&1
* * * * * cd /home/gavin && $(which python3) >> ~/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.

chmod +x <python file>
* * * * * /home/gavin/ >> ~/cron.log 2>&1



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
Gavin Wiener

Gavin Wiener

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