Tutorial

Cron jobs in Python.

To run a Python script on a schedule, put a line in your crontab that calls the Python interpreter by its full path. The usual failures are a missing virtualenv, a relative path, or output you never see. Here's how to avoid all three.

The crontab line

# Every day at 02:30
30 2 * * * /usr/bin/python3 /home/alice/jobs/report.py >> /home/alice/jobs/report.log 2>&1

Add it with crontab -e. Use the interpreter's full path (find it with command -v python3): cron's PATH is only /usr/bin:/bin, and none of your shell's settings (aliases, pyenv, conda init) are loaded.

Using a virtual environment

You don't need to "activate" a venv in cron. The Python docs say you can simply call the environment's own interpreter by its full path:

30 2 * * * /home/alice/app/.venv/bin/python /home/alice/app/report.py >> /home/alice/app/report.log 2>&1

That interpreter sees every package installed in the venv. Scripts installed into the venv (console entry points) also work by full path, because their first line points at the venv's Python.

Paths: cron doesn't start in your project folder

Cron starts jobs in the user's home directory, so open('data.csv') looks in the wrong place. Either change directory first:

30 2 * * * cd /home/alice/app && .venv/bin/python report.py >> report.log 2>&1

or build paths from the script's own location:

from pathlib import Path
BASE = Path(__file__).resolve().parent
data = BASE / "data.csv"

See the output

Cron emails whatever a job prints, and on most servers that mail goes nowhere. The >> file 2>&1 on the end saves both output and errors (including tracebacks) to a file. Python buffers output when it isn't writing to a terminal, so if you watch the log while a long job runs, add -u (python -u report.py) to see lines as they're printed. More in where are cron logs.

Environment variables and secrets

Variables you export in ~/.bashrc don't exist in cron. Set them at the top of the crontab (API_URL=https://…), or load a .env file inside the script. See cron environment variables.

Managing cron jobs from Python: python-crontab

To add or remove crontab entries from Python code (in an installer or deploy script, say), the python-crontab package reads and writes crontabs:

pip install python-crontab
from crontab import CronTab

cron = CronTab(user=True)                         # the current user's crontab
job = cron.new(command="/home/alice/app/.venv/bin/python /home/alice/app/report.py",
               comment="daily-report")
job.setall("30 2 * * *")
cron.write()                                       # nothing is saved until write()

cron.remove_all(comment="daily-report")            # later: remove it by its comment
cron.write()

Scheduling inside a Python program instead

If the job should run inside a long-running Python process rather than as a separate script, use a scheduler library: APScheduler (watch out: its from_crontab() numbers weekdays from Monday = 0), or Celery beat for Celery workers. For data pipelines with dependencies, see cron vs Airflow.

Check your schedule in the cron translator before saving, and if the job never runs, follow the troubleshooting checklist.

Sources

Checked against these official manuals and documentation. Last verified 18 November 2026.