How to automatically restart an app service after certain time?

那年仲夏 提交于 2020-02-15 10:53:42

问题


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?


回答1:


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




回答2:


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".




回答3:


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.




回答4:


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).



来源:https://stackoverflow.com/questions/45588260/how-to-automatically-restart-an-app-service-after-certain-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!