0
0
AirflowHow-ToBeginner ยท 4 min read

How to Use Cron Expression in Airflow for Scheduling

In Airflow, you use a cron expression in the schedule_interval parameter of a DAG to define when it runs. This expression follows standard cron syntax like "0 12 * * *" to run daily at noon.
๐Ÿ“

Syntax

A cron expression in Airflow uses five fields separated by spaces: minute, hour, day of month, month, and day of week. Each field controls when the DAG runs.

  • Minute: 0-59
  • Hour: 0-23
  • Day of Month: 1-31
  • Month: 1-12
  • Day of Week: 0-6 (Sunday=0)

Example: "0 12 * * *" means run at 12:00 PM every day.

python
schedule_interval = "0 12 * * *"
๐Ÿ’ป

Example

This example shows how to create a simple Airflow DAG that runs every day at 7:30 AM using a cron expression in schedule_interval.

python
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

with DAG(
    dag_id='daily_7_30_am',
    start_date=datetime(2024, 1, 1),
    schedule_interval='30 7 * * *',  # Runs daily at 7:30 AM
    catchup=False
) as dag:
    task = BashOperator(
        task_id='print_date',
        bash_command='date'
    )
Output
When the DAG runs at 7:30 AM, the task prints the current date and time in the Airflow logs.
โš ๏ธ

Common Pitfalls

Common mistakes when using cron expressions in Airflow include:

  • Using the wrong number of fields (Airflow expects 5 fields, not 6 like some cron versions).
  • Confusing day of week and day of month fields, which can cause unexpected schedules.
  • Forgetting that Airflow uses UTC time by default, so scheduled times may differ from your local time.
  • Using schedule_interval='@daily' if you want a simple daily run instead of a cron expression.

Always test your cron expression with an online cron tester to verify the schedule.

python
## Wrong: 6 fields (unsupported in Airflow)
schedule_interval = "0 12 * * * *"

## Right: 5 fields
schedule_interval = "0 12 * * *"
๐Ÿ“Š

Quick Reference

FieldAllowed ValuesDescriptionExample
Minute0-59Minute of the hour30
Hour0-23Hour of the day (24h)7
Day of Month1-31Day of the month*
Month1-12Month of the year*
Day of Week0-6Day of the week (Sunday=0)*
โœ…

Key Takeaways

Use a 5-field cron expression in the DAG's schedule_interval to set run times.
Airflow cron expressions follow standard syntax: minute, hour, day, month, day of week.
Airflow schedules use UTC time by default; adjust if needed for your timezone.
Test cron expressions with online tools to avoid scheduling errors.
For simple schedules, Airflow supports presets like '@daily' as alternatives.