---
title: "Python cron jobs: schedule a script with cron"
url: "https://cronmissed.com/learn/python-cron-jobs.md"
canonical_url: "https://cronmissed.com/learn/python-cron-jobs"
last_updated: "2026-11-10"
type: "conceptual"
summary: "Run a Python script from cron with absolute paths, a virtualenv and a log file, know when to use APScheduler or Celery beat, and get an alert if it stops."
related: ["https://cronmissed.com/learn.md", "https://cronmissed.com/guides/apscheduler.md", "https://cronmissed.com/guides/celery-beat.md", "https://cronmissed.com/learn/cron-syntax-cheat-sheet.md", "https://cronmissed.com/learn/cron-environment-and-path.md", "https://cronmissed.com/learn/cron-job-not-running.md", "https://cronmissed.com/snippets/python.md"]
---

# Python cron jobs: schedule a script with cron

> **For AI agents:** the complete index of CronMissed pages is [llms.txt](https://cronmissed.com/llms.txt). Every page is also served as markdown at its URL plus .md.

Run a Python script from cron with absolute paths, a virtualenv and a log file, know when to use APScheduler or Celery beat, and get an alert if it stops.

To run a Python cron job, add a crontab line that names the interpreter and the script by absolute path. Cron starts with a bare environment, so a script that works in your terminal can fail there until the paths, the virtualenv and the log file are spelled out.

## How do you schedule a Python script with cron?

Open the crontab with `crontab -e` and add one line: five schedule fields, the interpreter, the script, and a redirect for the output.

```
15 3 * * * /srv/app/.venv/bin/python /srv/app/jobs/nightly.py >> /var/log/nightly.log 2>&1
```

That runs `nightly.py` at 03:15 every day with the interpreter of the virtualenv in `/srv/app/.venv`. Naming the virtualenv's interpreter is enough to use its packages: there is nothing to activate. If the schedule needs a second look, paste it into the [cron expression translator](https://cronmissed.com/tools/cron-explainer.md), or read the [cron syntax cheat sheet](https://cronmissed.com/learn/cron-syntax-cheat-sheet.md).

## Why does a Python script work in the terminal but not in cron?

- **A different PATH.** Cron sets a short PATH, so `python` may be missing or point to another version. Use the full path from `which python3`.
- **No login environment.** Variables from your shell profile, such as API keys, are not set. Put them in the crontab above the line, or load them from a file in the script.
- **A different working directory.** Cron starts in the home directory of the user. Open files by absolute path, or `cd` first: `cd /srv/app && .venv/bin/python jobs/nightly.py`.
- **Percent signs.** In a crontab line, `%` starts standard input unless it is escaped as `\%`. A `date +%F` in the command breaks the line.

[Cron environment and PATH](https://cronmissed.com/learn/cron-environment-and-path.md) goes through the rest, and [cron job not running](https://cronmissed.com/learn/cron-job-not-running.md) is the full checklist.

## How do you keep the output of a Python cron job?

Cron mails any output to the owner of the crontab, and the mail is lost on a machine with no mail system. Redirect standard output and standard error to a file, as in the line above, and let the script write its own log records with the `logging` module. [Cron logs](https://cronmissed.com/learn/cron-logs.md) explains where cron itself records each start.

## When should you use APScheduler or Celery beat instead?

Cron starts a new process for every run, which suits a script that does one job and exits. When the work needs a long-running process, such as an application that holds a database pool or an in-memory cache, the schedule belongs inside it. [APScheduler](https://cronmissed.com/guides/apscheduler.md) runs jobs inside your own process, and [Celery beat](https://cronmissed.com/guides/celery-beat.md) hands scheduled tasks to a pool of workers. Both read cron-style schedules, so the expressions on this page carry over.

## How do you know the script ran?

Have the script report to a check when the work is done. The request below has a timeout and swallows its own errors, so a problem on the monitoring side can never fail the job:

```
import urllib.request

def report(suffix=""):
    try:
        urllib.request.urlopen("https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10" + suffix, timeout=10).read()
    except OSError:
        pass

def main():
    ...  # the nightly work
    report()

if __name__ == "__main__":
    try:
        main()
    except Exception:
        report("/fail")
        raise
```

Or leave the script alone and ping from the crontab line, which works for a script in any language:

```
15 3 * * * /srv/app/.venv/bin/python /srv/app/jobs/nightly.py && curl -fsS --max-time 10 --retry 5 -o /dev/null https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10
```

Create a check with the same schedule and CronMissed alerts you when the ping is late or the job reports a failure. The [crontab guide](https://cronmissed.com/guides/linux-crontab.md) walks through the setup.

## Questions

### How do I run a Python cron job every 5 minutes?

Use `*/5 * * * *` as the schedule, followed by the interpreter and script. Make sure a run can finish within 5 minutes, or two runs will overlap; see [overlapping cron runs](https://cronmissed.com/learn/overlapping-cron-runs.md).

### Do I need to activate the virtualenv in the crontab?

No. Running the interpreter inside the virtualenv, such as /srv/app/.venv/bin/python, uses that environment's packages without an activate step.

### Is there a cron job library for Python?

Cron itself is a system service, not a library. To read or edit crontab files from Python there are third-party packages, and to run schedules inside Python use APScheduler or Celery beat.

## Sources

- [crontab(5) manual page](https://man7.org/linux/man-pages/man5/crontab.5.html), read 22 October 2026
- [cron(8) manual page](https://man7.org/linux/man-pages/man8/cron.8.html), read 22 October 2026

## Related pages

- [APScheduler](https://cronmissed.com/guides/apscheduler.md)
- [Celery beat](https://cronmissed.com/guides/celery-beat.md)
- [Cron syntax cheat sheet](https://cronmissed.com/learn/cron-syntax-cheat-sheet.md)
- [Cron environment and PATH](https://cronmissed.com/learn/cron-environment-and-path.md)
- [Cron job not running checklist](https://cronmissed.com/learn/cron-job-not-running.md)
- [Python](https://cronmissed.com/snippets/python.md)

## Related

- [Cron monitoring explained: concepts and fixes](https://cronmissed.com/learn.md)
- [APScheduler cron jobs: schedule and monitor](https://cronmissed.com/guides/apscheduler.md)
- [Monitor Celery beat periodic tasks](https://cronmissed.com/guides/celery-beat.md)
- [Cron syntax cheat sheet: fields and examples](https://cronmissed.com/learn/cron-syntax-cheat-sheet.md)
- [Cron environment variables and PATH explained](https://cronmissed.com/learn/cron-environment-and-path.md)
- [Cron job not running: a checklist by system](https://cronmissed.com/learn/cron-job-not-running.md)
- [Ping CronMissed from Python](https://cronmissed.com/snippets/python.md)
