Bamboo: how to find if any build agent is running from script
Asked by Ninad Gawade:

I am relatively new to Bamboo engine. I am writing a script to blow away a cache on each build agent. However, before blowing away cache, I need to check if any build agent is performing any build. If yes, then my script will wait till it finished its job else it will straight away remove cache. Not sure how to check build-agents status from scripts.

Thanks.


Answers:
author: Ninad Gawade
body: Hello Rafael,

Thanks for responding. I am aware of the options you have provided in your response. These options will clear cache for all my builds. However, I need to do the clear my cache (or any other directory) only on certain event which I am going to control using a script.

Please find the snapshot herein to give you a better idea about my implementation. The highlighted line is the place where I need to check if my agent is currently idle or its working.

Just to give you a brief idea about this implementation: I have 10 build agents and on certain events, I need to clear sstate_cache ( other directories) from all the existing build agents. However, I need to make sure I am cleaning build agent only if the build agent is ideal at that time else I will wait till it get ideal.

Screen Shot 2016-12-21 at 2.52.02 PM.png

[](https://community.atlassian.com/t5/Bamboo-questions/Bamboo-how-to-find-if-any-build-agent-is-running-from-script/qaq-p/417192?attachment-id=1102){#link_116665e60f499f}  
[](/tnckb94959/attachments/tnckb94959/bamboo-questions/13167/1/Screen%20Shot%202016-12-21%20at%202.52.02%20PM.png){#display_116665e60f499f_0}

comments: author: rsperafico
body: If you are running a build against those 10 Bamboo agents, builds will only run if the the agent is available, otherwise will be sitting on queue. Once their are taken from queue, you could add a Script task to be executed as the first thing in the JOB to delete the caching you are referring to.

You could also run the following REST API call:

```
curl -k -u BAMBOO_ADMIN -H "Content-type: application/json" -X GET BAMBOO_BASE_URL/rest/api/latest/agent
```

As a response, you should get:

```
[{    "id": 4718593,    "name": "Agent A",    "type": "REMOTE",    "active": false,    "enabled": true,    "busy": false}, {    "id": 163841,    "name": "Default Agent",    "type": "LOCAL",    "active": true,    "enabled": true,    "busy": true}]
```

You should be looking at the **busy** state, if it is **true**, the agent in question is building something.


author: systematicguy
body: I was trying to use this api for very similar semaphoring reasons but to my horror it turns out that it can be out of sync for \~30-60 seconds (we are using Bamboo 6.6.2). I mean I start a build which sleeps for 60 seconds and start to poll

```
BAMBOO_BASE_URL/rest/api/latest/agent
```

And it says for the particular agent "busy": false for \~30 seconds before it starts to report true.

The same goes after the end of the build. How to ask the bamboo server nicely to report the most recent state?


author: rsperafico
body: Hello Ninad,

If possible, could you please provide more information regarding on the cache you are referring to?

Please, find below ways to clean working directory, if this is what you are looking for.

image2016-12-21 20:29:7.png

image2016-12-21 20:31:19.png

Kind regards,

Rafael  
[](https://community.atlassian.com/t5/Bamboo-questions/Bamboo-how-to-find-if-any-build-agent-is-running-from-script/qaq-p/417192?attachment-id=3047){#link_116665fabc42e5}  
[](/tnckb94959/attachments/tnckb94959/bamboo-questions/13166/1/image2016-12-21%2020%3A31%3A19.png){#display_116665fabc42e5_0}  
[](https://community.atlassian.com/t5/Bamboo-questions/Bamboo-how-to-find-if-any-build-agent-is-running-from-script/qaq-p/417192?attachment-id=3048){#link_116665fabc42e5_0}  
[](/tnckb94959/attachments/tnckb94959/bamboo-questions/13166/2/image2016-12-21%2020%3A29%3A7.png){#display_116665fabc42e5_2}


Tags: agent