Как запустить файл сценария bash в Airflow
У меня есть сценарий Bash, который создает файл (если он не существует), который я хочу запустить в Airflow, но при попытке это не удается. Как мне это сделать?
#!/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
и вот как я это называю в 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
2 ответа
Решение
Из учебника это нормально:
t2 = BashOperator(
task_id='sleep',
bash_command='sleep 5',
retries=3,
dag=dag)
Но вы передаете многострочную команду
create_command = """
./scripts/create_file.sh
"""
должно быть
create_command = "./scripts/create_file.sh"
Кроме того, вы также должны убедиться, что вы находитесь в правильном каталоге, чтобы избежать загадочных ошибок. Сделайте это, например, так:
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))
Из документации :
t2 = BashOperator(
task_id='bash_example',
# "scripts" folder is under "/usr/local/airflow/dags"
bash_command="scripts/test.sh",
dag=dag)