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 :
- Ensure your Python script is on the Azure VM and that you have Python installed.
- Go to
Start
>Windows Administrative Tools
>Task Scheduler
.
- 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".
- In the "Add arguments (optional)" field, type the path to your script, e.g.,
- Browse and select your Python executable (usually located in the Scripts folder of your Python installation, and named
- Click "Next".
- In the "Action" step, choose "Start a program".
- Click "Next".
- Choose the "When the computer starts" trigger.
- Click "Next".
- Name your task and provide a description if desired.
- In the Actions Pane, click on "Create Basic Task".
For Linux :
Upload Your Script: Ensure your Python script is on the Azure VM and that you have Python installed.
- Make Your Script Executable:
-
chmod +x /path/to/your/script.py
- Edit the
rc.local
File:- Open the
rc.local
file (or create it if it doesn't exist) using a text editor:
- Open the
-
sudo nano /etc/rc.local
- Before the
exit 0
line, add the path to your Python script: -
/usr/bin/python3 /path/to/your/script.py
- Ensure
rc.local
is Executable: -
sudo chmod +x /etc/rc.local
- Enable and Start
rc.local
Service: -
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