定期重启计划 <schedule>

概述

<periodicRestart> 元素的 <schedule> 集合指定应用程序池中工作进程重启之间的时间间隔。

兼容性

版本 说明
IIS 10.0 <schedule> 元素在 IIS 10.0 中未进行修改。
IIS 8.0 <schedule> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <schedule> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 <schedule> 元素是在 IIS 7.0 中引入的。
IIS 6.0 <schedule> 元素替换了 IIS 6.0 IIsApplicationPools 元数据库属性的部分内容

安装

<applicationPools> 集合包含在 IIS 7 的默认安装中。

操作方式

如何为应用程序池设置定期回收

  1. 打开“Internet Information Services (IIS)管理器”:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 徽标键,按字母 X,然后单击“控制面板”
      • 单击“管理工具”,然后双击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”
      • 双击“管理工具”,然后双击“Internet Information Services (IIS)管理器”
  2. 在“连接”窗格中,展开服务器名称,然后单击“应用程序池”

  3. 在“应用程序池”窗格中,选择要编辑的应用程序池

  4. 在“操作”窗格中,单击“回收…”
    Image of Application Pools pane showing Default App Pool highlighted.

  5. 在“编辑应用程序池回收设置向导”的“回收条件”页上,选择“固定间隔”部分中的至少一个选项,在相应的文本框中输入值,然后单击“下一步”
    Screenshot of Recycling Conditions page in Edit Application Pool Recycling Settings Wizard.

  6. (可选)在“编辑应用程序池回收设置向导”的“回收要记录的事件”页上,选择希望 IIS 在其发生时发送到事件日志的可配置回收事件和运行时回收事件,然后单击“完成”
    Image of Recycling Events to Log page in Edit Application Pool Recycling Settings Wizard.

配置

可以在 ApplicationHost.config 文件中的服务器级别配置 <schedule> 元素。

特性

无。

子元素

元素 说明
add 可选元素。 指定用于启动应用程序池定期回收的值。
clear 可选元素。 指定父级配置设置应从此级别的配置中删除。

配置示例

以下配置示例使用应用程序池 <add> 元素创建名为 Contoso 的新应用程序池。 <recycling> 元素配置应用程序池重启的日志记录,<periodicRestart> 元素配置应用程序池重启的时间,<processModel> 配置用于关闭和启动应用程序池中的工作进程的 shutdownTimeLimit 和 startupTimeLimit 属性,每个属性都配置为 30 秒。 如果超出这些时间限制,IIS 将终止工作进程。

<add name="Contoso">
   <recycling logEventOnRecycle="Schedule">
      <periodicRestart>
         <schedule>
            <clear />
            <add value="03:00:00" />
         </schedule>
      </periodicRestart>
   </recycling>
   <processModel identityType="NetworkService" shutdownTimeLimit="00:00:30" startupTimeLimit="00:00:30" />
</add>

代码示例

下面的代码示例将名为 Contoso 的应用程序池添加到 IIS 7 服务器,然后将应用程序池设置为每天在凌晨 3:00 回收。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso'].recycling.periodicRestart.schedule.[value='03:00:00']" /commit:apphost

还可以使用以下语法:

appcmd.exe add apppool /name:"Contoso"

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso'].recycling.periodicRestart.schedule.[value='03:00:00']" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
         ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();
         ConfigurationElement addElement = applicationPoolsCollection.CreateElement("add");
         addElement["name"] = @"Contoso";
         ConfigurationElement recyclingElement = addElement.GetChildElement("recycling");
         ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
         ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");
         ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
         addElement1["value"] = TimeSpan.Parse("03:00:00");
         scheduleCollection.Add(addElement1);
         applicationPoolsCollection.Add(addElement);
         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim applicationPoolsSection As ConfigurationSection = config.GetSection("system.applicationHost/applicationPools")
      Dim applicationPoolsCollection As ConfigurationElementCollection = applicationPoolsSection.GetCollection
      Dim addElement As ConfigurationElement = applicationPoolsCollection.CreateElement("add")
      addElement("name") = "Contoso"
      Dim recyclingElement As ConfigurationElement = addElement.GetChildElement("recycling")
      Dim periodicRestartElement As ConfigurationElement = recyclingElement.GetChildElement("periodicRestart")
      Dim scheduleCollection As ConfigurationElementCollection = periodicRestartElement.GetCollection("schedule")
      Dim addElement1 As ConfigurationElement = scheduleCollection.CreateElement("add")
      addElement1("value") = TimeSpan.Parse("03:00:00")
      scheduleCollection.Add(addElement1)
      applicationPoolsCollection.Add(addElement)
      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST");
var applicationPoolsCollection = applicationPoolsSection.Collection;

var addElement = applicationPoolsCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "Contoso";
var recyclingElement = addElement.ChildElements.Item("recycling");
var periodicRestartElement = recyclingElement.ChildElements.Item("periodicRestart");
var scheduleCollection = periodicRestartElement.ChildElements.Item("schedule").Collection;
var addElement1 = scheduleCollection.CreateNewElement("add");
addElement1.Properties.Item("value").Value = "03:00:00";
scheduleCollection.AddElement(addElement1);
applicationPoolsCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST")
Set applicationPoolsCollection = applicationPoolsSection.Collection

Set addElement = applicationPoolsCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "Contoso"
Set recyclingElement = addElement.ChildElements.Item("recycling")
Set periodicRestartElement = recyclingElement.ChildElements.Item("periodicRestart")
Set scheduleCollection = periodicRestartElement.ChildElements.Item("schedule").Collection
Set addElement1 = scheduleCollection.CreateNewElement("add")
addElement1.Properties.Item("value").Value = "03:00:00"
scheduleCollection.AddElement(addElement1)
applicationPoolsCollection.AddElement(addElement)

adminManager.CommitChanges()