Configure Azure DevOps pipeline agent to auto reboot after each job.

Sometimes you might want a cleanly started machine (not cleanly installed, mind you) for your pipeline job. For instance, if you are running UI tests. In some situations UI tests are very brittle and might be affected by a canceled or failed previous run. Is these circumstances, restarting the agent automatically after each job can be beneficial. This is now possible with the introduction of the --once parameter of the agent (more info here).

Start off by installing your agent as usual, and be sure to make it an interactive agent. Don’t forget to configure it to autologon, since automatically rebooting without that feature would stop our agent in its tracks rather fast. After you have done this you can add a custom cmd (you could name it customrun.cmd for instance) file to the root directory of the agent with the following contents:

call "C:\agent\run.cmd" --startuptype autostartup --once
shutdown /r /t 0 /f

If you run this file, the agent will start and the --once parameter will force it to close after the first job is finished. The shutdown command will then immediately restart the machine.

To have this file run during autologon instead of the default generated run command. You need to edit the registry as well. Start your registry editor and search for the following key:

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

Change the contents to something like the following (be sure to put in the full path to your own custom cmd file obviously):

C:\windows\system32\cmd.exe /D /S /C start "Agent with AutoLogon" "C:\agent\customrun.cmd"


One thought on “Configure Azure DevOps pipeline agent to auto reboot after each job.

  1. Michael A.

    Simple Solution, if you dont want to reboot your system but set the Agent back to “Listen”:

    Add the following else block in the run.cmd of your selected agent to check for errorcode 0 instead of 4 and set the reset time to a time of your choosing (i chose 15s):

    rem Return code 4 means the run once agent received an update message.
    rem Sleep 5 seconds to wait for the update process finish and run the agent again.
    if ERRORLEVEL 4 (
    timeout /t 5 /nobreak > NUL
    “%~dp0bin\Agent.Listener.exe” run %*
    )else ( rem the added code
    if ERRORLEVEL 0 (
    timeout /t 15 /nobreak > NUL
    “%~dp0bin\Agent.Listener.exe” run %*
    )
    )

    Reply

Leave a Reply to Michael A. Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.