How to configure Airflow dag to run at specific time on daily basis?
Asked Answered
L

4

19

How to configure the Airflow dag to execute at specified time on daily basis no matter what happens, something exactly like crons.

I know that similar behaviour could be obtained using TimeSensor, but in this case it depends upon the sensor tasks and which might conflict with the dag execution time.

Example: With sensor approach if I have sensor to run at 0 hour 15th minutes but if dag is executed at later then my task is delayed, so even for the sensor approach I need to make sure that the Dag is executed on right time.

So how to make sure that Dag is executed at specified time?

Loupe answered 27/2, 2016 at 10:58 Comment(0)
S
21

To start a DAG for example everyday on 2:30 AM in the morning you can do the following:

DAG(
   dag_id='dag_id',
   # start date:28-03-2017
   start_date= datetime(year=2017, month=3, day=28),
   # run this dag at 2 hours 30 min interval from 00:00 28-03-2017
   schedule_interval='30 2 * * *')

Before configuring the schedule the interpretation of the cron interval can verified and tested here: https://crontab.guru/

Shouldst answered 29/3, 2017 at 19:59 Comment(5)
airflow 1.8.0, schedule_interval='0 24 * * *' doesn't work for me, got a nuclear bomb blast ascii art and the following error message: CroniterBadCronError: [0 24 * * * ] is not acceptable, out of rangeDyson
Reference for the cron job format #14710757Shouldst
@Dyson that is exactly why he suggested to check the cron on the linkClarethaclaretta
How do we set up DAG to run every other day at 11 PM? Not daily.Diskin
crontab.guru/#0_23_1-31/2__ , unix.stackexchange.com/a/16094/345195Shouldst
S
0

@ruhong I see in a comment you are wondering how to do every other day. The Month is the third parameter and if you do a 2 30 */2 * * it will run every other day (at 2:30am). It calculates it a little weird sometimes depending on the month. You can force it to run even or odd days by specifying the range:

# Will only run on odd days:
2 30 1-31/2 * * command

# Will only run on even days:
2 30 2-30/2 * * command
Stablish answered 18/5, 2021 at 12:32 Comment(0)
F
0
dag = DAG(
    'run_bash_command',
    default_args=default_args,
    schedule_interval=timedelta(days=1, hours=7),
    description='A simple DAG to run a bash command every day at 7 AM',
)


##To run the Dag every day at 7 A.M
Foretopgallant answered 20/5 at 13:11 Comment(0)
B
-1

You can set the schedule_interval to a string cron expression when you instantiate a DAG:

schedule_interval='0 * * * *'

BaseOperator documentation

Beseem answered 11/3, 2016 at 22:22 Comment(3)
Yeah, I figured it out but the problem is, its not working as expected. If I set the cron string to "1,2,3 * * * *" It it executes minutes 1 at minute2 and 2 at minute 3 and 3 will be executed at minute 1of next hour.Loupe
More details of the issue with cron expression hereLoupe
Yeah, that's because airflow doesn't execute on the interval, it runs the dag after that interval has finished. Since you specify minutes 1, 2, and 3, it will run at the end of those periods, on 2, 3, and 1. That's explained in the docs hereSaxhorn

© 2022 - 2024 — McMap. All rights reserved.