Cron Job in Python

Python offers multiple libraries for scheduling tasks, including APScheduler for in-process scheduling, Celery for distributed task queues, and the simple schedule library for basic needs.

How to Set Up Cron Jobs in Python

1

Install APScheduler

APScheduler is the most versatile Python scheduler

Code
pip install apscheduler
2

Create a cron-style schedule

Use CronTrigger for cron expressions

Code
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger

def my_job():
    print("Running scheduled task")

scheduler = BlockingScheduler()
scheduler.add_job(
    my_job,
    CronTrigger.from_crontab('*/5 * * * *')  # Every 5 minutes
)
scheduler.start()
3

Alternative: Use schedule library

For simpler interval-based scheduling

Code
import schedule
import time

def job():
    print("Running task...")

schedule.every(5).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

Python Cron Limitations vs CronUptime

Python Limitations

  • Requires a running Python process or Celery worker
  • Celery needs Redis/RabbitMQ infrastructure
  • APScheduler jobs are lost on restart (unless using job stores)
  • No built-in HTTP endpoint monitoring
  • Complex setup for distributed systems

CronUptime Advantages

  • No Python process to maintain
  • Works with Flask, Django, FastAPI endpoints
  • Automatic retries on failure
  • Execution logs and history
  • Zero infrastructure management

Why Use CronUptime Instead?

While Python cron jobs work for basic use cases, managing infrastructure for scheduled tasks adds complexity. CronUptime offers a simpler, serverless alternative.

No Infrastructure

We handle execution. No servers to maintain.

Reliable Timing

Built on Cloudflare for 99.9%+ uptime.

Any Endpoint

Works with any HTTP endpoint on any platform.

Frequently Asked Questions

For simple in-process scheduling, use APScheduler. For distributed systems, Celery with Celery Beat is recommended. For basic intervals, the schedule library works well.
You can use system crontab (crontab -e on Linux), APScheduler within your app, or a managed service like CronUptime that calls your Python web endpoint on schedule.
APScheduler runs tasks in the same process and is simpler to set up. Celery is a distributed task queue requiring Redis/RabbitMQ but scales better for heavy workloads.

Quick Reference

Cron Expression:

*/5 * * * *

Human Readable:

every 5 minutes

Cron Jobs on Other Platforms

Try CronUptime Free

Schedule HTTP requests without managing Python infrastructure.

Try it now - Free

Create a cron job running every 5 minutes. No sign-up required.

The URL that will be called. You can also paste a curl command here.

Runs for 7 daysSign in for permanent