You have created Self-Hosted private build agent on Ubuntu for Azure pipelines. But you may not need the VM running 24/7. To save cost on this VM, you can create a schedule to stop and start the VM automatically outside office hours. In doing so you would also need to make sure that when the VM is restarted, the VSTS build agent is also auto started to avoid any manual step. We will see
>>>>How to automatically start/stop existing Linux VM.
>>>>Then How to automatically start VSTS build agent on it.
Auto Start/Stop Linux VM:
>>Login to azure portal and search service “Automation Account”
>>Click +Add to create Automation Account
>>Provide Name, Resource Group, Location and Create Azure Run As account. Then click Create
>>Once created, it will look like below screen
>>Click on “TestAccount” and then select Start/Stop VM and click on “Manage the Solution” link
>>Click +Add and then search “Start/Stop VMs during off-hours”, it will open screen like below, click Create on it
>>Create a new OMS workspace by clicking on “Create New Workspace” and provide the required values as shown below and click OK [make sure the resource group and location is same as that of Automation account]
>>Then select Automation Account and then click on configuration and click OK to create the schedule as shown, you can change it later as well-
>>Click Create to add the solution. It will add the solution and also it will create the schedule as shown below
>>You can select the Start and Stop schedule to set the time, timezone and frequency.
Based on the schedule the VM will be stopped and started. Now lets see how to auto start the VSTS build agent once VM is restarted. Check this for more details – Azure Automation
Auto Start Build Agent:
>>Login to Liunx VM using PuTTY
>>We will use cron job to schedule the auto start of build agent when Linux VM reboots. Run crontab -e command as shown and select 3
>>It will open the file to add the task to be run by cron. Add this command “@reboot /home/bpsadmin/run.sh &” and then save the file.
>>Now when the Linux VM reboots, it will start the build agent automatically.