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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With