python - BashOperator 不运行 bash 文件 apache Airflow

标签 python airflow

我刚开始使用 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/

相关文章:

python - samtools - dyld : Library not loaded: @rpath/libcrypto. 1.0.0.dylib

flask - Airflow Webserver 访问日志的位置

python - Airflow 中的成功邮件

python - SparkSubmitOperator 部署模式

python - 在 Python 或 PIL 中是否有一种简单的方法来调整/扩展仅包含零的图像大小?

python - 导入错误 : TensorBoard

apache-spark - 通过 Airflow 调度在 Kubernetes 上运行的 Spark 作业

airflow - Apache Airflow : initdb vs resetdb

python - 加载 MySQLdb 时出错

python - 如何获取 CSV 文件中的列并将文本分隔并保存到 python 中的其他列