Azure virtual machine startup script for every restart.

vsi_pranav 161 Reputation points
2021-04-17T06:52:09.32+00:00

Hello,
I am using the Azure virtual machine scale set service. In which I have multiple Ubuntu 18.04 virtual machines. Currently, I am using the Custom Script for Linux extension to run a specific script when the VM starts. From what I have read so far from the documentation is, this script gets executed only once when the VM is provisioned for the first time.

Now I need some extension or service which will run every time the machine is restarted and not just once when it is provisioned. I am aware that restarting the VM via reboot command from the terminal and restarting it through the Azure portal are two different events, and I need the extension/service to support both of these scenarios.

I read about the Cloud-init, but could not quite figure it out and I am not sure whether it is the right service according to my scenario.

Kindly, please help me if you know of any such a service or suggest any way if this can be achieved in the linux system.

Note: The scale set is placed behind the standard public load balancer.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,035 questions
Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
450 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.5K Reputation points MVP Volunteer Moderator
    2021-04-17T08:59:55.267+00:00

    Hi @vsipranav-0854 ,

    if the script should run on every restart, no matter if the restart is initiated by Azure Portal or via reboot command (inside VM) maybe this helps:
    https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up

    As far as I know "Azure" does not recognize a reboot via reboot command. Using an "in-guest VM" approach to execute a script might be your best option.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. vsi_pranav 161 Reputation points
    2021-06-09T10:42:28.947+00:00

    Hello @Andreas Baumgarten
    Apologies for delayed answer response.
    Updates for the answer are as follows.

    1) Azure Custom Script Extension for Linux worked fine at the VM provisioning time as expected in most of the time. Although I did get 403 Forbidden error sometimes. The only workaround I could find out was to uninstall the current extension and add new Custom Script Extension with the same script. This solution was not sustainable.

    2) Second solution was to use Cloud-Init Script recommended in azure documentation. This script worked fine the context of Plain New Azure Virtual Machine with the Ubuntu 18.04 LTS image. But I am using the specialized shared gallery image as base image for my azure virtual machine scale set. That image is nothing but Ubuntu 18.04 LTS at the base and on top of that I have installed my work environment i.e. npm, node js, mediasoup libraries etc. But somehow cloud-init did not work in this scenario.

    3) The third solution which worked fine for me is to write a cron job for every reboot which will eventually run my shell script. So far this solution is working good and my script runs on every reboot.

    Thanks again for the reply.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.