BashOperator没有运行bash文件apache airflow

时间:2017-02-15 20:23:38

标签: python airflow

我刚开始使用apache气流。我试图从气流运行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作为答案,但它不起作用

我犯错误的地方?

3 个答案:

答案 0 :(得分:13)

在.sh之后添加一个空格它应该工作 气流的confluence page中提到了这一点

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) 

答案 1 :(得分:1)

只使用脚本路径,不使用“sh”: create_command =“/ home /ubuntu / test / inst / scripts / test.sh”

还要确保“airflow”用户有权执行“test.sh”脚本。

答案 2 :(得分:0)

尝试没有" sh"只需将命令设置为" home / ubuntu / test / inst / scripts / test.sh"