Python 如何在Airflow中运行bash脚本文件
我有一个bash脚本,它创建了一个文件(如果它不存在的话),我想在Airflow中运行该文件,但当我尝试时失败了。我该怎么做Python 如何在Airflow中运行bash脚本文件,python,airflow,Python,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 以下是我在《气流》中对它的称呼:
#!/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
以下是我在《气流》中对它的称呼:
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))
从:
在.sh之后添加空格:“./scripts/create_file.sh”有时您可能会收到错误:
此操作失败,因为找不到Jinja模板
,以克服脚本末尾添加空格
,不确定是什么导致了这种行为:ref:@KarolSudol-Its,因为气流会检查您通过的线的末端,如果它以.sh结尾,它会尝试将其视为模板。空格会打断该检查,并且不会将其视为模板。不过,我还是不明白为什么。感谢这个空格提示,Nooot会不会明白,如果语法定义说在定义的操作符(如{operator}})的开始和结束处添加空格,其余看起来不错。
t2 = BashOperator(
task_id='bash_example',
# "scripts" folder is under "/usr/local/airflow/dags"
bash_command="scripts/test.sh",
dag=dag)