gpt4 book ai didi

python - BashOperator 不运行 bash 文件 apache Airflow

转载 作者:太空狗 更新时间:2023-10-29 20:44:39 27 4
gpt4 key购买 nike

我刚开始使用 apache airflow。我正在尝试从 Airflow 运行 test.sh 文件,但它不起作用。

以下是我的代码,文件名为test.py

import os
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta


default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2015, 6, 1),
'email': ['airflow@airflow.com'],
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
# 'queue': 'bash_queue',
# 'pool': 'backfill',
# 'priority_weight': 10,
# 'end_date': datetime(2016, 1, 1),
}

dag = DAG('test', default_args=default_args)

# t1 and t2 are examples of tasks created by instantiating operators
t1 = BashOperator(
task_id='print_date',
bash_command='date',
dag=dag)

create_command = "sh home/ubuntu/test/inst/scripts/test.sh"

if os.path.exists(create_command):
t2 = BashOperator(
task_id= 'cllTest',
bash_command=create_command,
dag=dag
)
else:
raise Exception("Cannot locate {}".format(create_command))

t2.set_upstream(t1)

当我运行 python ~/airflow/dags/test.py 时,它不会抛出任何错误。

但是,当我运行 airflow list_dag 时,它会抛出以下错误:

[2017-02-15 20:20:02,741] {__init__.py:36} INFO - Using executor SequentialExecutor
[2017-02-15 20:20:03,070] {models.py:154} INFO - Filling up the DagBag from /home/ubuntu/airflow/dags
[2017-02-15 20:20:03,135] {models.py:2040} ERROR - sh home/ubuntu/test/inst/scripts/test.sh
Traceback (most recent call last):
File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/airflow/models.py", line 2038, in resolve_template_files
setattr(self, attr, env.loader.get_source(env, content)[0])
File "/home/ubuntu/anaconda2/lib/python2.7/site-packages/jinja2/loaders.py", line 187, in get_source
raise TemplateNotFound(template)
TemplateNotFound: sh home/ubuntu/test/inst/scripts/test.sh

我试过用How to run bash script file in Airflow对于答案,它不起作用

我哪里出错了?

最佳答案

在 .sh 后加一个空格就可以了confluence page 中提到了这一点 Airflow

t2 = BashOperator(
task_id='sleep',
bash_command="/home/batcher/test.sh", // This fails with `Jinja template not found` error
#bash_command="/home/batcher/test.sh ", // This works (has a space after)
dag=dag)

关于python - BashOperator 不运行 bash 文件 apache Airflow ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com