Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow BashOperator doesn't work but PythonOperator does

I seem to have a problem with BashOperator. I'm using Airflow 1.10 installed on CentOS in a Miniconda environment (Python 3.6) using the package on Conda Forge.

When I run airflow test tutorial pyHi 2018-01-01 the output is "Hello world!" as expected.

However, when I run airflow test tutorial print_date 2018-01-01 or airflow test tutorial templated 2018-01-01 nothing happens.

This is the Linux shell output:

(etl) [root@VIRT02 airflow]# airflow test tutorial sleep 2015-06-01 [2018-09-28 19:56:09,727] {__init__.py:51} INFO - Using executor SequentialExecutor [2018-09-28 19:56:09,962] {models.py:258} INFO - Filling up the DagBag from /root/airflow/dags

My DAG configuration file, which is based on the Airflow tutorial, is shown below.

from airfl ow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta

import test

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2010, 1, 1),
    'email_on_failure': False,
    'email_on_retry': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
}

dag = DAG(
    'tutorial',
    'My first attempt',
    schedule_interval=timedelta(days=1),
    default_args=default_args,
)

# t1, t2 and t3 are examples of tasks created by instantiating operators
t1 = BashOperator(
    task_id='print_date',
    bash_command='date',
    dag=dag)

t2 = BashOperator(
    task_id='sleep',
    bash_command='sleep 5',
    retries=3,
    dag=dag)

templated_command = """
    {% for i in range(5) %}
        echo "{{ ds }}"
        echo "{{ macros.ds_add(ds, 7)}}"
        echo "{{ params.my_param }}"
    {% endfor %}
"""

t3 = BashOperator(
    task_id='templated',
    bash_command=templated_command,
    params={'my_param': 'Parameter I passed in'},
    dag=dag)

t4 = BashOperator(
    task_id='hi',
    bash_command = 'test.sh',
    dag=dag,
)

t5 = PythonOperator(
    task_id='pyHi',
    python_callable=test.main,
    dag=dag,
)


t2.set_upstream(t1)
t3.set_upstream(t1)
like image 475
Joe Heffer Avatar asked Oct 02 '18 09:10

Joe Heffer


1 Answers

Technically it's not that the BashOperator doesn't work, it's just that you don't see the stdout of the Bash command in the Airflow logs. This is a known issue and a ticket has already been filed on Airflow's issue tracker: https://issues.apache.org/jira/browse/AIRFLOW-2674

The proof of the fact that BashOperator does work is that if you run your sleep operator with

airflow test tutorial sleep 2018-01-01

you will have to wait 5 seconds before it terminates, which is the behaviour you'd expect from the Bash sleep command.

like image 76
Alessandro Cosentino Avatar answered Oct 12 '22 10:10

Alessandro Cosentino