共用方式為


定期重新開機排程排程 <>

概觀

專案的 <schedule> 集合 <periodicRestart> 會指定應用程式集區中背景工作進程重新開機之間的時間間隔。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <schedule>
IIS 8.0 未在 IIS 8.0 中修改專案 <schedule>
IIS 7.5 未在 IIS 7.5 中修改專案 <schedule>
IIS 7.0 元素 <schedule> 是在 IIS 7.0 中引進。
IIS 6.0 元素 <schedule> 會取代 IIS 6.0 IIsApplicationPools Metabase 屬性的部分。

安裝程式

<applicationPools>集合包含在 IIS 7 的預設安裝中。

作法

如何設定應用程式集區的定期回收

  1. 開啟 [Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager
  2. 在 [ 連線] 窗格中,展開伺服器名稱,然後按一下 [ 應用程式集區]。

  3. 在 [ 應用程式集區 ] 窗格中,選取您要編輯的應用程式集區。

  4. 在 [ 動作 ] 窗格中,按一下 [ 回收...
    [應用程式集區] 窗格的影像,其中已醒目提示預設應用程式集區。

  5. 在 [編輯應用程式集區回收設定精靈] 的 [回收條件] 頁面上,選取 [ 固定間隔 ] 區段中至少其中一個選項,在適當的文字方塊中輸入值,然後按 [ 下一步]。
    [編輯應用程式集區回收設定精靈] 中 [回收條件] 頁面的螢幕擷取畫面。

  6. (選擇性) 在 [編輯應用程式集區回收設定精靈] 的 [回收事件至記錄檔] 頁面上,選取您要 IIS 在事件發生時傳送至事件記錄檔的可設定回收事件和執行時間回收事件,然後按一下 [ 完成]。
    [編輯應用程式集區回收設定精靈] 中 [將事件回收至記錄] 頁面的影像。

組態

<schedule>您可以在 ApplicationHost.config 檔案的伺服器層級設定 元素。

屬性

無。

子元素

元素 描述
add 選擇性項目。 指定值,以啟動應用程式集區的定期回收。
clear 選擇性項目。 指定應該從此層級的組態中移除父層級組態設定。

組態範例

下列組態範例會使用應用程式集 <add> 區元素來建立名為 Contoso 的新應用程式集區。 元素 <recycling> 會設定應用程式集區重新開機的記錄、 <periodicRestart> 專案會在應用程式集區重新開機時設定,而 <processModel> 元素會設定 shutdownTimeLimitstartupTimeLimit 屬性,以關閉應用程式集區中的背景工作進程,每 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 來設定這些設定時,請務必將 認可 參數 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()