Error calling BashOperator: Bash command failed

2024/11/15 10:46:17

Here are my dag file and BashOperator task:

my_dag = { 
dag_id = 'my_dag',
start_date = datetime(year=2017, month=3, day=28),
schedule_interval='01***',
}my_bash_task = BashOperator(
task_id="my_bash_task",
bash_command=bash_command,
dag=my_dag)

bash_command = "/home/jak/my_projects/workflow_env/repo_workflow/db_backup_bash.sh ""

Following this answer I even gave a space after the bash file to avoid TemplateNotFound error. But while running this task gave me this error:airflow.exceptions.AirflowException: Bash command failed.

bash_command file contents are:

#!/bin/bash
DATABASE=db_name
FILE=$DATABASE-`date +%F-%H%M%S`.backup
export PGPASSWORD=password
pg_dump -h localhost -p 5432 -U developer -F c -b -v -f ~/Dropbox/database_backup/location/$FILE db_name
unset PGPASSWORD

However instead of pointing the bash_command to the bash file writing the command in multi line string works:

bash_command = """
DATABASE=db_name
FILE=$DATABASE-`date +%F-%H%M%S`.backup
export PGPASSWORD=password
pg_dump -h localhost -p 5432 -U developer -F c -b -v -f ~/Dropbox/database_backup/location/$FILE db_name
unset PGPASSWORD
"""

Because of this I am assuming that the error is not because bash commands. I even tried replacing #!/bin/bash in the bash file with #!/bin/sh, that did not work either.

I ran sh db_back_up_bash.sh from terminator and it works fine.

Update The Actual code:

bash_file_location_to_backup_db = '{{"/home/jak/my_projects/workflow_env/repo_workflow/db_backup_bash.sh"}}'
# bash_file_location_to_backup_db = "/home/jak/my_projects/workflow_env/repo_workflow/db_backup_bash.sh "
bash_command = """
DATABASE=ksaprice_scraping
FILE=$DATABASE-`date +%F-%H%M%S`.backup
export PGPASSWORD=password
pg_dump -h localhost -p 5432 -U developer -F c -b -v -f ~/Dropbox/database_backup/ksaprice/$FILE ksaprice_scraping
unset PGPASSWORD
"""backup_scraped_db_in_dropbox_task = BashOperator(task_id="backup_scraped_db_in_dropbox_task",# bash_command=bash_command,# this works finebash_command=bash_file_location_to_backup_db,#this give error :airflow.exceptions.AirflowException: Bash command faileddag=dag_crawl
)

Error trace:

[2017-04-11 20:02:14,905] {bash_operator.py:90} INFO - Output:
2017-04-11 20:02:14,905 | INFO| root : Output:
[2017-04-11 20:02:14,906] {bash_operator.py:94} INFO - /tmp/airflowtmp7FffJ2/backup_scraped_db_in_dropbox_taskQ6IVxm: line 1: /home/jak/my_projects/workflow_env/repo_workflow/db_backup_bash.sh: Permission denied
2017-04-11 20:02:14,906 | INFO| root : /tmp/airflowtmp7FffJ2/backup_scraped_db_in_dropbox_taskQ6IVxm: line 1: /home/jak/my_projects/workflow_env/repo_workflow/db_backup_bash.sh: Permission denied
[2017-04-11 20:02:14,906] {bash_operator.py:97} INFO - Command exited with return code 126
2017-04-11 20:02:14,906 | INFO| root : Command exited with return code 126
[2017-04-11 20:02:14,906] {models.py:1417} ERROR - Bash command failed
Traceback (most recent call last):File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/models.py", line 1374, in runresult = task_copy.execute(context=context)File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in executeraise AirflowException("Bash command failed")
AirflowException: Bash command failed
2017-04-11 20:02:14,906 | ERROR| root : Bash command failed
Traceback (most recent call last):File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/models.py", line 1374, in runresult = task_copy.execute(context=context)File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in executeraise AirflowException("Bash command failed")
AirflowException: Bash command failed
[2017-04-11 20:02:14,907] {models.py:1441} INFO - Marking task as FAILED.
2017-04-11 20:02:14,907 | INFO| root : Marking task as FAILED.
[2017-04-11 20:02:14,947] {models.py:1462} ERROR - Bash command failed
2017-04-11 20:02:14,947 | ERROR| root : Bash command failed
Traceback (most recent call last):File "/home/jak/my_projects/workflow_env/bin/airflow", line 28, in <module>args.func(args)File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/bin/cli.py", line 585, in testti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/utils/db.py", line 53, in wrapperresult = func(*args, **kwargs)File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/models.py", line 1374, in runresult = task_copy.execute(context=context)File "/home/jak/my_projects/workflow_env/local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in executeraise AirflowException("Bash command failed")
airflow.exceptions.AirflowException: Bash command faile
Answer

I consider this a bug in airflow, jinja should not expect .sh files to contain template information in a BashOperator.

I got around it by putting the command into a format Jinja will interpret correctly:

bash_command = '{{"/home/jak/my_projects/workflow_env/repo_workflow/db_backup_bash.sh"}}'

https://en.xdnf.cn/q/71960.html

Related Q&A

Match unescaped quotes in quoted csv

Ive looked at several of the Stack Overflow posts with similar titles, and none of the accepted answers have done the trick for me.I have a CSV file where each "cell" of data is delimited by …

Creating RDF file using csv file as input

I need to convert a csv file to rdf with rdflib, I already have the code that reads the csv but I do not know how to convert it to rdf.I have the following code:import csv from rdflib.graph import Grap…

Explicit vertex position in python graph-tool

I am using python graph-tool. To draw graphs, it uses graph_draw function. I want to send vertex positions explicitly to dot engine. It turns out that I can pass a property map named pos. I tried defin…

Add jar to pyspark when using notebook

Im trying the mongodb hadoop integration with spark but cant figure out how to make the jars accessible to an IPython notebook.Here what Im trying to do:# set up parameters for reading from MongoDB via…

how do I create a python list with a negative index

Im new to python and need to create a list with a negative index but have not been successful so far.Im using this code:a = [] for i in xrange( -20, 0, -1 ):a[i] = -(i)log.info(a[{i}]={v}.format(i=i, v…

Select subset of Data Frame rows based on a list in Pandas

I have a data frame df1 and list x:In [22] : import pandas as pd In [23]: df1 = pd.DataFrame({C: range(5), "B":range(10,20,2), "A":list(abcde)}) In [24]: df1 Out[24]:A B C 0 a …

convert csv to json (nested objects)

I am new to python, and I am having to convert a csv file to json in following format:CSV File :firstname, lastname, email, customerid, dateadded, customerstatus john, doe, [email protected], 124,26/11…

How can I read exactly one response chunk with pythons http.client?

Using http.client in Python 3.3+ (or any other builtin python HTTP client library), how can I read a chunked HTTP response exactly one HTTP chunk at a time?Im extending an existing test fixture (writt…

ValueError: cannot reindex from a duplicate axis in groupby Pandas

My dataframe looks like this:SKU # GRP CATG PRD 0 54995 9404000 4040 99999 1 54999 9404000 4040 99999 2 55037 9404000 4040 1556894 3 55148 9404000 4040 1556894 4 55254 94…

How to calculate class weights of a Pandas DataFrame for Keras?

Im tryingprint(Y) print(Y.shape)class_weights = compute_class_weight(balanced,np.unique(Y),Y) print(class_weights)But this gives me an error:ValueError: classes should include all valid labels that can…