How do I configure jenkins to restart the agent for each build?

I tried to use "launch method: launch agent via execution of command on the master" in combintation with "availability: take this agent online when in demand, and offline when idle" but it has two drawbacks:

  1. (not critical) build starts with a delay of about one minute.
  2. (critical) the node goes offline only a minute after the end of the build, and if another build starts at that time, the same still running agent will be used (even if there are several similarly configured free nodes).

There is even a plugin (scripted cloud plugin) that, according to the description, should be suitable, but it does not work, probably because its code is outdated.

  • Curious ... why do you need the agent to be restarted for each build ? At first glance that sounds suspicious to me where you might be trying to solve the wrong problem. But without context it's hard to evaluate.
    – Newtopian
    Commented Dec 7, 2018 at 13:47
  • @Newtopian, each build requires a clean image, and each start of agent creates clean image. if the agent does not restart, a dirty image is used. that's the problem. Commented Dec 7, 2018 at 14:24
  • hmmm... I see... my first reflex here would be to inverse this dependency where the agent would start the image and run the build on it. I'll move the the rest in an answer.
    – Newtopian
    Commented Dec 7, 2018 at 17:07
  • Aleksandr, what was your solution for this? I have a similar use-case in that I need to cycle my agents after a build.
    – Ungeheuer
    Commented Mar 21, 2023 at 4:41

2 Answers 2


Slave restarts the agent itself after each build? You can use Conditional BuildStep plugin to restart it on a defined condition.


I still dont see how starting an agent creates a clean image. Expanding on your setup would help us provide better answer.

That said, from what I can gather here the following assumption comes to mind :

  • You have agents pre-populated on a VM image. Starting this image will in turn start the agent in which you wish to start the build.

So you now have a chicken vs egg problem where you need the agent running to start the job but you need to start the agent as the job starts.

Why not have another agent who's job is to start the image. This new agent is no longer sensitive to the image cleanliness and can run indefinitely.

My approach here would take this a step further and not have the agent as part of the image altogether, but rather have the agent first start the image, provision it with whatever is needed for the build in some way, and then run the build.

A simple shared folder on the image with the host could do the work. Tools like Vagrant could also be of use. But without more information it's really difficult to go further.

This answer on stack overflow could also contain some help in this regard. Basically spins-up a new docker image on demand.

Another possible way with your current setup, make sure the build always ends with a shutdown of the image. This way the next job will be sure to use a fresh image as it should never survive the build. You could even test fort his situation and fail the build outright if you detect the image was re-cycled between builds rather than re-launched.

  • "I still dont see how starting an agent creates a clean image." VMs with readonly root keep the FS in memory and on cycling anything not part of the readonly image are lost. I have a similar use-case. I have a writable overlay image for my agents with a backing file that contains the OS and everything else. Once a build is complete, I want to shutdown the agent so that hooks in libvirt can run and re-provision the writable overlay. So, to answer the unasked question, there are a few ways that restarting gets a clean environment.
    – Ungeheuer
    Commented Mar 21, 2023 at 4:40
  • >VMs with readonly root keep the FS in memory and on cycling anything not part of the readonly image are lost. ... Ha! interesting yes ! thanks for the info
    – Newtopian
    Commented Mar 23, 2023 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.