我有一个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
最佳答案
在本教程中,这是可以的:
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))
关于python - 如何在Airflow中运行bash脚本文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40146934/