簡體   English   中英

如何使用 Airflow 在 python 中運行 bash 腳本

[英]How to run bash script in python using Airflow

我有一個python函數

def running_dump():
    with open('dags/scripts/shell_scripts/daily_pg_dump.sh', 'rb') as file:
        script = file.read()
        print(script)
    subprocess.call(script, shell=True)

和一個 shell 文件daily_pg_dump.sh

PGPASSWORD='*******' pg_dump -h ***** -p ***** -U ***** -d * -t table_1 > dags/data_bucket/table_1_backup.sql

氣流標簽

pg_dump_to_storage = PythonOperator(
        task_id='task_1',
        python_callable=running_dump,
        dag=dag
    )

當我使用 Airflow 調用 python 函數時,shell 腳本似乎沒有運行,因為table_1_backup.sql沒有被創建。 相反,我得到的返回值為:0但沒有出現錯誤。 我錯過了什么?

如果你想從 Airflow 運行 bash 腳本,你可以使用BashOperator而不是PythonOperator

從文檔中的這個例子來看,你的情況是:

from airflow.operators.bash import BashOperator

running_dump = “path/to/daily_pg_dump.sh ” # note the space after the script's name

pg_dump_to_storage = BashOperator(
   task_id='task_1', 
   bash_command=running_dump,
   dag=dag,
)

注意:當您不在bash 腳本中使用模板時,您只需要腳本路徑名后面的空格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM