How to run bash script file in Airflow
Asked Answered
C

2

26

I have a bash script that creates a file (if it does not exist) that I want to run in Airflow, but when I try it fails. How do I do this?

#!/bin/bash
#create_file.sh

file=filename.txt

if [ ! -e "$file" ] ; then
    touch "$file"
fi

if [ ! -w "$file" ] ; then
    echo cannot write to $file
    exit 1
fi

and here's how I'm calling it in Airflow:

create_command = """
 ./scripts/create_file.sh
"""
t1 = BashOperator(
        task_id= 'create_file',
        bash_command=create_command,
        dag=dag
)

lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 83, in execute
    raise AirflowException("Bash command failed")
airflow.exceptions.AirflowException: Bash command failed
Careen answered 20/10, 2016 at 6:27 Comment(0)
T
25

From the tutorial this is OK:

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

But you're passing a multi-line command to it

create_command = """
 ./scripts/create_file.sh
"""

should be

create_command = "./scripts/create_file.sh "

Moreover, you also have to make sure that you are in the correct directory to avoid cryptic errors. Do it like this for example:

create_command = "./scripts/create_file.sh "
if os.path.exists(create_command):
   t1 = BashOperator(
        task_id= 'create_file',
        bash_command=create_command,
        dag=dag
   )
else:
    raise Exception("Cannot locate {}".format(create_command))
Tinge answered 20/10, 2016 at 6:34 Comment(6)
add space after .sh : "./scripts/create_file.sh "Cyma
sometimes you might receive error: This fails with Jinja template not found, in order to overcome add space at the end of the script, not sure what drives this behaviour: ref: cwiki.apache.org/confluence/display/AIRFLOW/Common+PitfallsCyma
@KarolSudol Its because airflow checks the end of the line you pass in and if it ends in .sh it tries to treat it as a template. A space breaks that check and it doesn't treat it as a template. I still can't figure out the why, though.Angevin
thanks for that space tip - would noooot have figured that out myselfAngellaangelle
jinga syntax definition says to add spaces in start & end of defined operator like {{ operator }}, rest looks good.Eugenioeugenius
template_ext = ('.sh', '.bash',) these extensions are defined as template extension in the Operator class. So they are treated as templates rather than file execution.Scarcely
U
2

From the documentation:

t2 = BashOperator(
    task_id='bash_example',
    # "scripts" folder is under "/usr/local/airflow/dags"
    bash_command="scripts/test.sh",
    dag=dag)
Unfrock answered 25/3, 2021 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.