如何在Ai中运行bash脚本文件

2024-04-24 04:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个bash脚本,它创建了一个文件(如果它不存在的话),我想在airlow中运行,但是当我尝试它时失败了。我该怎么做?

#!/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

Tags: 文件脚本bashifshcreatecommandfi
1条回答
网友
1楼 · 发布于 2024-04-24 04:30:10

从教程中可以看出:

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))

相关问题 更多 >