2017-02-15 91 views
4

我剛開始使用apache airflow。我試圖從氣流運行test.sh文件,但它不工作。BashOperator不運行bash文件apache airflow

以下是我的代碼,文件名是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': ['[email protected]'], 
    '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〜/通風/的DAG/test.py,它不會引發任何錯誤。

然而,當我運行氣流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的答案,這是行不通的

我在哪裏犯錯?

回答

5

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

嘗試沒有 「SH」 剛纔設置的命令爲 「家庭/ Ubuntu的/測試/安裝/腳本/ test.sh」

0

只使用腳本路徑,沒有 「SH」: create_command =「/ home/ubuntu/test/inst/scripts/test.sh「

還要確保」airflow「用戶有權執行」test.sh「腳本。