Run a Python Script Every Hour with Cron
To run a Python script at the top of every hour, point cron at the interpreter and the script with absolute paths. The most common failure is using a virtualenv — call the venv's python directly instead of relying on 'source activate'.
The crontab line
0 * * * * /usr/bin/python3 /home/me/app/task.py
Runs at minute 0 of every hour. Use absolute paths for both the interpreter and the script.
Using a virtualenv
Don't source activate in cron. Call the venv interpreter directly — it sets up its own environment:
0 * * * * /home/me/app/venv/bin/python /home/me/app/task.py
Fix relative paths inside the script
Cron starts in the home directory, so relative file paths break. Either use absolute paths in the code, or cd first:
0 * * * * cd /home/me/app && /home/me/app/venv/bin/python task.py >> run.log 2>&1
FAQ
What cron schedule runs a Python script every hour?
0 * * * * /usr/bin/python3 /path/to/script.py runs at the start of every hour. Use full paths for both python3 and the script.
How do I run a script inside a virtualenv from cron?
Call the venv's interpreter directly: /path/venv/bin/python script.py. This activates the environment implicitly, so you never need 'source activate' which doesn't work in a non-interactive cron shell.
Why can't my Python cron job find its files?
Cron runs from your home directory, so relative paths fail. Use absolute paths in the code, or prefix the command with 'cd /project/dir &&'.