How to trigger python script on an Azure windows VM when my Azure vm is started

Benoy Mammen Mathew 20 Reputation points
2023-09-21T10:53:00.3866667+00:00

I have created an azure virtual machines which starts and stops at particular times. This is performed by Azure Logic Apps.

How can I run a python script on my azure VM when the VM is started?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
Microsoft 365 and Office SharePoint For business Windows
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
0 comments No comments
{count} votes

Accepted answer
  1. Konstantinos Passadis 19,586 Reputation points MVP
    2023-09-21T11:38:17.65+00:00

    Hello @Benoy Mammen Mathew !

    Welcome to Microsoft QnA!

    I understand you need to execute a python script once the Azure VM is starting

    The feature of Task Scheduler is the easy way on Windows or CRON on Linux

    For Windows :

    1. Ensure your Python script is on the Azure VM and that you have Python installed.
    • Go to Start > Windows Administrative Tools > Task Scheduler.
    1. Create Basic Task:
      • In the Actions Pane, click on "Create Basic Task".
        • Name your task and provide a description if desired.
          • Click "Next".
            • Choose the "When the computer starts" trigger.
              • Click "Next".
                • In the "Action" step, choose "Start a program".
                  • Click "Next".
                    • Browse and select your Python executable (usually located in the Scripts folder of your Python installation, and named python.exe).
                      • In the "Add arguments (optional)" field, type the path to your script, e.g., C:\path\to\your\script.py.
                        • Click "Next", review your settings, and click "Finish".

    For Linux :

    Upload Your Script: Ensure your Python script is on the Azure VM and that you have Python installed.

    1. Make Your Script Executable:
    2. chmod +x /path/to/your/script.py
    3. Edit the rc.local File:
      • Open the rc.local file (or create it if it doesn't exist) using a text editor:
    4. sudo nano /etc/rc.local
    5. Before the exit 0 line, add the path to your Python script:
    6. /usr/bin/python3 /path/to/your/script.py
    7. Ensure rc.local is Executable:
    8. sudo chmod +x /etc/rc.local
    9. Enable and Start rc.local Service:
    10. sudo systemctl enable rc-local sudo systemctl start rc-local.service

    Your Python script should now run whenever the VM starts.

    You can also leverage Azure Managed Identity if you want the script to execute and Integrate for example a Storage Account , or Azure SQL or another Sevice

    Read more here :

    https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview


    I hope this helps!

    Kindly mark the answer as Accepted and Upvote in case it helped!

    Regards


0 additional answers

Sort by: Most helpful

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.