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?
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.
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
Save-AzureRmProfile -Path "E:\azureprofile.json" returns an error but the command Save-AzureRmContext-Path "C:\script.json" gives the same output as that of of the Save-AzureRmProfile -Path "E:\azureprofile.json".
You can also do this using LogicApps or RunBooks. Here is an example that uses Logic Apps: https://blog.aggregatedintelligence.com/2020/01/restarting-web-app-using-logic-apps.html
Basically, you create a trigger, and then you add a "Azure RM Invoke Action". The settings needed arent well documented, so I documented it in my blog post. This is basically the simplest way to make sure it happens on a schedule and brings together what has been previously discussed with regards to the REST API (the Azure RM action is using it under the covers), web-job (dont need to create one, as LogicApp will run it for you).