Chuyển đến nội dung

Bài 14 · Scheduling and Dependencies

Airflow Schedules: Cron Expressions, Presets and Timetables

Learn how to schedule Airflow DAGs with cron expressions, presets such as @daily, timedelta intervals, None for manual runs and custom timetables, with time zone tips.

The schedule argument tells Airflow when to create DAG runs. It accepts several kinds of values: cron strings, presets, a timedelta, None, assets (events) and custom timetables. This lesson covers the time-based ones in detail, including time zones, which cause more production surprises than any other detail.

The schedules are Airflow arguments and read-only here. A runnable cron stand-in below lets you check what a cron expression means by listing the next times it fires.

Cron expressions

A cron expression has five fields:

TEXT
┌──────── minute        (0-59)
│ ┌────── hour          (0-23)
│ │ ┌──── day of month  (1-31)
│ │ │ ┌── month         (1-12)
│ │ │ │ ┌ day of week   (0-6, Sunday = 0 or 7)
│ │ │ │ │
* * * * *

Examples:

Expression Meaning
0 6 * * * Every day at 06:00
*/15 * * * * Every 15 minutes
30 2 * * 1-5 02:30 on weekdays
0 0 1 * * Midnight on the first of each month
0 9 * * MON Mondays at 09:00
0 */4 * * * Every 4 hours, on the hour
Python
with DAG(dag_id="weekday_report", start_date=datetime(2025, 1, 1),
         schedule="30 2 * * 1-5", catchup=False) as dag:
    ...

Presets

Airflow understands short names:

Preset Equals Meaning
@once n/a Exactly one run
@continuous n/a A new run starts as soon as the last finishes
@hourly 0 * * * * Top of every hour
@daily 0 0 * * * Midnight every day
@weekly 0 0 * * 0 Midnight Sunday
@monthly 0 0 1 * * Midnight on the 1st
@yearly 0 0 1 1 * Midnight on 1 January

timedelta: fixed intervals

Python
from datetime import timedelta

with DAG(dag_id="every_90_minutes", start_date=datetime(2025, 1, 1),
         schedule=timedelta(minutes=90), catchup=False) as dag:
    ...

A timedelta produces runs spaced exactly that far apart, counted from start_date onward. Cron cannot express "every 90 minutes" because 90 does not divide a day evenly, so use timedelta (or a custom timetable) for such cases.

None: manual only

schedule=None means "never create runs automatically". Use it for DAGs you trigger by hand, from the API or from another DAG.

Cron semantics in Airflow 3

In Airflow 2, a cron string created data intervals: the run for the interval 1 March to 2 March started on 2 March at the end of the interval. In Airflow 3 a plain cron string uses the trigger timetable: a run is created at each cron time, with no interval attached. If you want the older behaviour, ask for it explicitly:

Python
from airflow.timetables.interval import CronDataIntervalTimetable

with DAG(
    dag_id="classic_interval",
    start_date=datetime(2025, 1, 1),
    schedule=CronDataIntervalTimetable("0 6 * * *", timezone="Europe/Oslo"),
) as dag:
    ...

and for trigger semantics with a time zone:

Python
from airflow.timetables.trigger import CronTriggerTimetable

schedule = CronTriggerTimetable("0 6 * * *", timezone="Europe/Oslo")

The next lesson explains intervals and the logical date in depth, because this choice decides which dates your task sees.

Time zones

By default Airflow works in UTC. Cron 0 6 * * * means 06:00 UTC. To schedule in local time, give the DAG a time-zone-aware start_date or build the timetable with timezone=:

Python
import pendulum

with DAG(
    dag_id="oslo_morning",
    start_date=pendulum.datetime(2025, 1, 1, tz="Europe/Oslo"),
    schedule="0 6 * * *",
    catchup=False,
) as dag:
    ...

Daylight saving time bites here. At the spring change a local 02:30 does not exist; at the autumn change 02:30 happens twice. Airflow's timetables handle this, but UTC is the simplest choice for data pipelines, and you can still display local time in the UI.

The first run and start_date

The earliest run is determined by start_date and the schedule. With catchup=False Airflow creates only the latest due run when the DAG is first unpaused (details in Catchup). Always use a fixed, past start_date.

Stand-in: next run times of a cron expression

This stand-in implements a small cron matcher for the five fields (*, numbers, ranges, lists and */n steps) and lists the next firing times from a start moment. It is a teaching tool, not Airflow's engine.

Python Stand-in: when does this cron fire?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try your own expression, for instance 0 9 * * MON will not work in this stand-in because names are not supported; use 0 9 * * 1.

Stand-in: timedelta schedule

A timedelta schedule is just repeated addition. This stand-in shows why 90 minutes cannot be a cron expression:

Python Stand-in: every 90 minutes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The times drift across the day (00:00, 01:30, 03:00, ...) and the next day starts at 12:00, which a single cron line cannot produce.

Choosing a schedule

  • Business reports: cron at a fixed local time, with a time zone.
  • Pipelines chained to other DAGs: consider asset-based scheduling instead of guessing times.
  • "Every N units" that does not divide evenly: timedelta.
  • Ad-hoc jobs: None.
  • Streaming-like batches: @continuous with care.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which cron expression runs at 02:30 on weekdays?

    Fields are minute, hour, day of month, month, day of week: 30 2 * * 1-5.
  2. 2. What does schedule=None mean?

    None disables automatic runs, which suits manual and externally triggered DAGs.
  3. 3. Why use timedelta(minutes=90) instead of cron for an every-90-minutes job?

    Cron fields repeat per hour/day boundary, so irregular periods need a fixed interval.
  4. 4. What time zone does Airflow use by default for schedules?

    UTC is the default; use a timezone-aware start_date or timetable for local time.

Summary

  • schedule accepts cron strings, presets, timedelta, None, timetables and assets.
  • Airflow 3 runs plain cron strings with trigger semantics; use CronDataIntervalTimetable for the classic interval behaviour.
  • Time zones default to UTC; mind daylight saving time if you schedule in local time.
  • Pick fixed past start_date values and think about whether events or time should drive each DAG.

Next: Data intervals and the logical date

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.