Azure App Services: Automate Application restart using Web Job


Use Case 

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.

NoteYou need to be Global admin on active directory to create the native AD appliction.

When you run this script, you will get prompt to provide Subscription Name, Password and then log in.

After completion, you will get below information in output:

untitled3-1024x509

NotePlease keep a note of th.s 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 output of 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.

untitled3-1024x509

  • 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:

 

Comments (1)

  1. Very helpful Article, thanks for sharing.

Skip to main content