To create a DAG function in Airflow that runs a Bash command every day at 7 A.M, you can follow these steps:
First, you need to define the DAG object with the DAG class from the airflow module. You will need to provide a unique ID for the DAG, a start date, and a schedule interval.
The schedule interval is defined using a cron expression, which is a string of fields separated by spaces, representing a schedule in time. In this case, you can use the cron expression ‘0 7 * * *’, which means the DAG will run every day at 7 A.M.
Next, you can define a BashOperator, which is a type of task in Airflow that runs a Bash command. You will need to provide a unique ID for the task and the Bash command that you want to run.
Finally, you can connect the task to the DAG by adding it to the DAG object.
Yogesh Baghel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.bash import BashOperator
default_args = {
'owner': 'airflow',
'start_date': datetime(2024, 5, 20),
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
dag = DAG(
'run_bash_command',
default_args=default_args,
schedule_interval=timedelta(days=1, hours=7),
description='7 A.M run dag',
)
run_bash_command = BashOperator(
task_id='run_bash_command',
bash_command='echo "Hello, Airflow!"',
dag=dag,
)
Yogesh Baghel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.