Terminate Bamboo Job with Python Exception
Asked by Billy Sharpe:

Hello all,  

I have a Bamboo job that runs a Python script, followed by a PowerShell script. What I am trying to figure out, is if the python script throws an exception (like below), how can I make the bamboo job terminate so it does not run the PowerShell script? I tried passing sys.exit(1) but bamboo still reads it as 0.  

except Exception as error:  
print(f"FAILED with the following error: {error} in the file: {filename}")  
sys.exit(1)

```
build	03-Jan-2024 16:18:49	FAILED with the following error: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). in the file: Afilename.xlsxsimple	03-Jan-2024 16:18:49	Finished task 'Run Python: CreateUploadForm' with result: Success
```

Thank you!

Billy


Answers:
author: Shashank Kumar
body: Hello Billy,

Welcome to Atlassian Community

If i understand correctly you have a single script task which contains Python and Powershell scripts.

You can try to use **set -e**at the beginning of the script task like below

```
#!/bin/bash
set -e
```

Bamboo will terminate the task when it sees any error and won't execute the next commands.

You can read similar information on this thread [https://community.atlassian.com/t5/Bamboo-questions/Bamboo-does-not-respect-exit-status-of-docker-compose/qaq-p/1197430?utm_source=atlcomm\&utm_medium=email\&utm_campaign=kudos_answer\&utm_content=topic](https://community.atlassian.com/t5/Bamboo-questions/Bamboo-does-not-respect-exit-status-of-docker-compose/qaq-p/1197430?utm_source=atlcomm&utm_medium=email&utm_campaign=kudos_answer&utm_content=topic)

Regards,

Shashank Kumar

**\*\*please don't forget to Accept the answer if your query was answered\*\***

comments: author: Billy Sharpe
body: Hello!  

Thanks for the info.   

Below is my current bamboo task list.

What I am looking to do is, if the script that runs python throws an exception, the powershell after it does not run.

According to bamboo, despite the exception being thrown, the script ran "successfully' and it continues running the powershell script.   
![bamboo.png](https://community.atlassian.com/t5/image/serverpage/image-id/298950i10B7D91A32541093/image-size/large?v=v2&px=999 "bamboo.png")

I should mention that my bamboo servers are windows, not linux if that matters.

Where do I add the -e?

Is it here?

![bamboo2.png](https://community.atlassian.com/t5/image/serverpage/image-id/298954i9E5ADC01CA2EDCE3/image-size/large?v=v2&px=999 "bamboo2.png")

Thanks again!


author: Shashank Kumar
body: Hello Billy,

Thanks for providing the clarification regarding the structure of your Job, now my understanding is clear.

Basically you have 1 task which is Python script and if this Task fails, you don't want another Task which is Powershell script to be executed.

In your case somehow Bamboo is returning exit code as 0, hence marking your Python script task as success.

**set -e**option will not work here as it is for Linux and your scripts are getting executed in Powershell environment.

I am not an expert in Windows shell programming, you can try couple of Options.

**Option 1:**

You'll need to exit with the exit code if it's non zero manually, you can try something

```
if ($lastexitcode -ne 0) { exit $lastexitcode }
```

**Option 2:**

You can try the below

```
echo "Exiting with return code: " %ERRORLEVEL%
exit %ERRORLEVEL%
```

You can read more about these at

<https://community.atlassian.com/t5/Bamboo-questions/How-do-you-capture-a-task-error-during-a-build/qaq-p/1238630>

<https://stackoverflow.com/questions/15777492/why-are-my-powershell-exit-codes-always-0>

Regards,

Shashank Kumar

**\*\*please don't forget to Accept the answer if your query was answered\*\***