Defining a BashOperator with @task.bash
A Bash task allows you to specify any given shell command or script and add it to an Airflow workflow. This can be a great start to implementing Airflow in your environment.
As such, you've been running some scripts manually to clean data (using a script called cleanup.sh) prior to delivery to your colleagues in the Data Analytics group. As you get more of these tasks assigned, you've realized it's becoming difficult to keep up with running everything manually, much less dealing with errors or retries. You'd like to implement a simple script as an Airflow operator.
The Airflow dag and task objects have already been imported.
This exercise is part of the course
Introduction to Apache Airflow in Python
Exercise instructions
- Use the
@task.bashdecorator to define the task. - Use the method name
cleanup_task. - Make the task return
cleanup.sh.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
@dag(
dag_id='analytics_dag',
start_date=datetime(2026,1,1)
)
def analytics_dag():
# Specify a Bash task
@____
def ____():
return '____'
# Run the task
cleanup_task()
analytics_dag()