【问题标题】:How to automatically restart an app service after certain time?如何在一定时间后自动重启应用服务?
【发布时间】:2017-08-09 10:35:28
【问题描述】:

如何在 24 小时后自动重启应用服务?如何通过 web 作业来安排应用服务在特定时间自动重启?

【问题讨论】:

  • 你有关于这个线程的任何更新吗?如果有用请点赞。

标签: azure-web-app-service


【解决方案1】:

您可以通过创建 Web 作业并放置 PowerShell 脚本来停止和启动 Web 应用程序来实现此目的。

要执行 Azure 应用服务的启动/停止操作,Web 作业应有权访问您的订阅。它还需要您的 Azure 配置文件。

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

创建 PowerShell 以停止/启动应用服务

创建一个新文件夹并将下载的发布配置文件放入 上一步。

创建一个 PowerShell 并保存为 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>'

将此添加到您的应用服务 Web 作业部分并通过创建 cron 表达式根据您的要求运行。

参考:Azure App Services: Automate Application restart using Web Job

【讨论】:

  • 命令 Save-AzureRmProfile -Path "E:\azureprofile.json" 返回一个错误,指出 "Save-AzureRmProfile" 未被识别为 cmdlet、函数、脚本文件或可操作文件的名称程序。
  • 您使用的是哪个版本的 Powershell? Save-AzureRmProfile 适用于版本 3.7。您可以使用以下 cmdlet 检查 Azure PowerShell 版本。 Get-Module -ListAvailable -Name Azure -Refresh 如果您使用的是最新版本,则可以使用 Save-AzureRmContext -Path "E:\azureprofile.json"。参考:github.com/Azure/azure-powershell/blob/preview/documentation/…
  • 将 Select-AzureRmProfile 替换为 Save-AzureRmContext。
【解决方案2】:

我们也可以使用Azure Rest API 来做到这一点。关于如何获取访问令牌请参考azure document

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

WebJob 接受以下文件类型:

.cmd、.bat、.exe(使用 windows cmd)

.ps1(使用 powershell)

.sh(使用 bash)

.php(使用 php)

.py(使用python)

.js(使用节点)

.jar(使用 java)

如果可以使用 C#,则有一个使用 .Net 库的example

【讨论】:

    【解决方案3】:

    您也可以使用 LogicApps 或 RunBooks 来执行此操作。下面是一个使用逻辑应用的示例:https://blog.aggregatedintelligence.com/2020/01/restarting-web-app-using-logic-apps.html

    基本上,您创建一个触发器,然后添加一个“Azure RM 调用操作”。所需的设置没有很好的记录,所以我在我的博客文章中记录了它。这基本上是确保它按计划发生的最简单方法,并将之前讨论过的关于 REST API(Azure RM 操作在幕后使用它)、网络作业(不需要创建一个,因为 LogicApp 将为您运行它)。

    【讨论】:

      【解决方案4】:

      Save-AzureRmProfile -Path "E:\azureprofile.json" 返回错误,但命令 Save-AzureRmContext-Path "C:\script.json" 提供与 Save-AzureRmProfile -Path 相同的输出E:\azureprofile.json"。

      【讨论】:

      • 请在原始问题中包含更新,而不是发布会使社区感到困惑的答案。
      猜你喜欢
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2019-08-23
      • 1970-01-01
      相关资源
      最近更新 更多