Azure App Services: Automate Application restart using Web Job
An application is hosted on Azure as App Service and you want to restart your application on a scheduled basis.
You can achieve this by creating a web job and placing a PowerShell script to STOP and START the web app. Please follow the steps below to implement this for your web app:
Get Service Principle Id for silent authentication
To perform START/STOP operation on Azure App Service, the job should have access to your subscription. Please run below Power-Shell script to create a native AD application from which we will get service principal ID that will be used to do silent authentication. This is a one time job.
Note : You need to be a Global admin on Active Directory to create the native AD application.
When you run this script, you will get prompt to provide Subscription Name, Password and then log in.
After completion, you will get information in output:
Note : Please keep a note of this information, we'll use it in following steps.
Create PowerShell to STOP/START App Service
- Create a PowerShell and save as run.ps1
- Update Service Principal Id, Tenant Id, Subscription Id and Password values using the output of the previous step.
If the application is running on multiple instances, then you can use below script to leverage Advanced Application restart. In this case below script will restart worker process on each instance one by one. This can help in minimizing the downtime.
Create a scheduled web job
- Create a zip file which contains both the files (ps1 and azureprofile.json).
- Go to App Service -> Web Job and then create a triggered web job by uploading the zip file.
- Make sure that Always on is enabled on the App Service where you are deploying this web job. Always on is required for triggered web jobs.
Note: Please follow the link below for information about scheduling web job using CRON expression: