We have a self hosted agent running our builds. We have configured it to auto shutdown everyday. In the morning, we always have to go and run the pipeline agent. Is there a way we can make it run everyday, especially each time we do the restart?
We tried adding the agent service to Windows Task Scheduler with a startup trigger, but it doesn’t seem to start correctly when the machine restarts. We expected the agent to automatically register and start running as soon as the machine is powered on.
We have a self hosted agent running our builds. We have configured it to auto shutdown everyday. In the morning, we always have to go and run the pipeline agent. Is there a way we can make it run everyday, especially each time we do the restart?
We tried adding the agent service to Windows Task Scheduler with a startup trigger, but it doesn’t seem to start correctly when the machine restarts. We expected the agent to automatically register and start running as soon as the machine is powered on.
Share Improve this question asked Mar 17 at 15:22 Talha RashidTalha Rashid 13 Answers
Reset to default 0You can:
Add schedule to your build: Configure schedules for pipelines
Configure your agent as a service: Run as a service - Windows
Based on the current info, it seems that you configured the self-hosted agent to run interactively. To starts the agent automatically, you can configure it to run as a service as mentioned by @Shamrai Aleksander.
Go to the root directory of your self-hosted agent. Run
.\config remove
to remove the old configuration.Reconfigure the agent to run as a service.
Run
.\config.cmd
and follow the guidance.When you see
Enter run agent as service? (Y/N)
, input Y.
See the detailed info from Interactive vs. service.
You can have this done by enabling this two flags in your self-hosted agent command/script.
--runAsService: Configures the agent to run as a service.
--runAsAutoLogon: runs the agent each time after restarting the machine.
In one of my previous project. Here is the script I built specifically for this purpose.
PowerShellCopy
# Define variables
$AgentPackageUrl = "https://vstsagentpackage.azureedge/agent/3.238.0/vsts-agent-win-x64-3.238.0.zip"
$AgentPackagePath = "$env:TEMP\agent.zip"
$AgentExtractPath = "C:\agent"
# Set Azure DevOps anization URL
$OrganizationUrl = "https://dev.azure/dammyboss"
# Set Personal Access Token
$PAT = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Set agent name
$AgentName = "xxx"
# Set pool name
$PoolName = "xxxxxx"
# Download agent package
Invoke-WebRequest -Uri $AgentPackageUrl -OutFile $AgentPackagePath
# Extract agent package
Expand-Archive -Path $AgentPackagePath -DestinationPath $AgentExtractPath
# Navigate to agent directory
Set-Location -Path $AgentExtractPath
# Run configuration script with parameters
& .\config.cmd --url $OrganizationUrl --token $PAT --pool $PoolName --agent $AgentName --unattended
In this script, I have added the necessary flags to run as a service and restart at logon. You would not have to worry about having to log in to the machine to start the agent.
I took the liberty to test this on one of my VMs just a few mins ago, see the result below. I restarted it and confirmed the service start running post restart.