Python
Cron
Scheduler
Task Automation
apscheduler

How do I get a Cron like scheduler in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want cron-like scheduling in Python, the practical answer is usually APScheduler. It gives you interval scheduling, fixed times, and cron expressions without depending on the host operating system, which makes it useful for scripts, services, and cross-platform applications.

When to Use Python Instead of System Cron

System cron is still a strong option for simple Unix jobs, especially when you only need to run a script every hour or every night. A Python scheduler becomes more useful when:

  • the application must run on Windows as well as Linux
  • jobs need to share in-process state
  • you want dynamic schedules stored in code or a database
  • you need multiple scheduling styles inside one program

For these cases, APScheduler is a better fit than trying to re-create cron logic manually with loops and sleep.

Install and Run a Basic Scheduler

Start with the package:

bash
pip install apscheduler

The simplest runtime for a command-line process is BlockingScheduler.

python
1from datetime import datetime
2from apscheduler.schedulers.blocking import BlockingScheduler
3
4
5def job():
6    print(f"Running at {datetime.now():%Y-%m-%d %H:%M:%S}")
7
8
9scheduler = BlockingScheduler()
10scheduler.add_job(job, "interval", minutes=1)
11scheduler.start()

This behaves like a small scheduler process. It stays alive and runs job every minute until you stop it.

Use a Cron Trigger

If you want cron semantics such as "every weekday at 08:30", use the cron trigger instead of interval scheduling.

python
1from apscheduler.schedulers.blocking import BlockingScheduler
2
3
4def send_report():
5    print("Daily report sent")
6
7
8scheduler = BlockingScheduler(timezone="America/Toronto")
9scheduler.add_job(send_report, "cron", day_of_week="mon-fri", hour=8, minute=30)
10scheduler.start()

This is close to a cron entry, but you configure it in Python rather than in a system crontab. The explicit timezone is important for predictable behavior during daylight saving changes.

Background Schedulers for Web Apps and Services

If the scheduler must run alongside an application instead of owning the whole process, use BackgroundScheduler.

python
1import time
2from apscheduler.schedulers.background import BackgroundScheduler
3
4
5def cleanup():
6    print("Cleaning up temporary files")
7
8
9scheduler = BackgroundScheduler()
10scheduler.add_job(cleanup, "cron", hour=2, minute=0)
11scheduler.start()
12
13for _ in range(3):
14    print("Main application still running")
15    time.sleep(5)
16
17scheduler.shutdown()

This lets the rest of the program continue while scheduled jobs run in the background. It is a better design for long-lived services, but it also means you must think about thread safety if jobs touch shared state.

schedule vs APScheduler

The schedule library is smaller and easier to read for tiny scripts:

python
1import schedule
2import time
3
4
5def job():
6    print("Hello from schedule")
7
8
9schedule.every().day.at("10:00").do(job)
10
11while True:
12    schedule.run_pending()
13    time.sleep(1)

This is fine for lightweight automation, but it lacks the flexibility and production features of APScheduler, such as richer triggers, job stores, and stronger control over execution policies.

Designing Reliable Scheduled Jobs

A scheduler is only half the problem. The jobs themselves must be safe to run repeatedly. Make sure each task is idempotent where possible, logs failures clearly, and does not assume the process can never restart.

Also think about overlap. If one job may run longer than its schedule interval, configure limits or locking. A task that starts every minute but takes three minutes to finish can quickly create duplicate work unless you handle concurrency explicitly.

Common Pitfalls

  • Using a Python scheduler when plain system cron would be simpler. If a static Unix job is enough, cron is still the cleanest option.
  • Forgetting that the Python process must stay alive. A scheduler inside a script does nothing after the script exits.
  • Ignoring timezone configuration. Scheduled times drift in surprising ways around daylight saving transitions.
  • Running long jobs without considering overlap. Re-entrant jobs can corrupt shared state or overload external systems.
  • Embedding a scheduler in a web process without deciding who owns execution. Multiple app instances may each run the same job.

Summary

  • 'APScheduler is the usual choice for cron-like scheduling in Python applications.'
  • Use BlockingScheduler for standalone scheduler processes.
  • Use BackgroundScheduler when jobs must run alongside another application.
  • Prefer cron triggers for calendar-based schedules such as weekdays at a fixed time.
  • Treat job design, overlap, and timezone handling as part of the scheduling solution.

Course illustration
Course illustration

All Rights Reserved.