Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically restart an app service after certain time?

How to automatically restart an app service after 24 hours? How to schedule the app service to restart automatically at a specific time through the use of web jobs?

like image 560
Apurva Avatar asked Aug 09 '17 10:08

Apurva


2 Answers

You can achieve this by creating a web job and placing a PowerShell script to stop and start the web app.

To perform start/stop operation of Azure App Service, the web job should have access to your subscription. It also requires your Azure profile.

Login-AzureRmAccount
Save-AzureRmProfile -Path "E:\azureprofile.json"

To Create PowerShell to stop/start App Service

Create a new folder and place the publish profile downloaded in previous step.

Create a PowerShell and save as run.ps1

$ProgressPreference= "SilentlyContinue"
Select-AzureRmProfile -Path "azureprofile.json"
Select-AzureRmSubscription -SubscriptionId '<subscriptionId>'
Stop-AzureRmWebApp -Name '<AppService-Name>' -ResourceGroupName '<Resource-Group-Name>'
Start-AzureRmWebApp -Name '<AppService-Name>' -ResourceGroupName '<Resource-Group-Name>'

Add this in your App service web job section and run based on your requirement by creating a cron expression.

Reference: Azure App Services: Automate Application restart using Web Job

like image 182
AshokPeddakotla-MSFT Avatar answered Jan 03 '23 13:01

AshokPeddakotla-MSFT


We also could do that with Azure Rest API. About how to get access token please refer to azure document.

POST /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/restart?api-version=2016-08-01&softRestart&synchronous={softRestart&synchronous}

The following file types are accepted by WebJob:

.cmd, .bat, .exe (using windows cmd)

.ps1 (using powershell)

.sh (using bash)

.php (using php)

.py (using python)

.js (using node)

.jar (using java)

If C# is possible, there is a example using .Net library.

like image 40
Tom Sun - MSFT Avatar answered Jan 03 '23 13:01

Tom Sun - MSFT