Recycling Application Pools using WMI in IIS 6.0

Posted: Aug 30, 2006  24 comments  

Average Rating

Share this Post

Can one recycle an application from a script in IIS 6.0?

 

We (MS) certainly improved our story around ensuring reliablity and performance with the release of IIS 6.0.  IIS 6.0 has had numerous positive comments around the process archictecture and also the server's ability to maintain it's own health.  The building blocks of the reliability of IIS 6.0 is around the creation of this term called "Application Pools."  Without sounding nuts, let me just phrase this as meaning "Application Pools are a single or collection of application chosen to be isolated based on application demands."

That is why we here so often at Microsoft that administrators are continually frustrated by the overall lack of guidance in the area of recycling.  What are the guidelines for recycling?  For example, is state (application or session) lost?  Are connections reset or are friendly errors such as 503's returned?

To save time, check out this webcast we did on this topic for more information to answers relating to this question...

Recycling IIS 6.0 Applications: The Good, the Bad, and the Ugly

The powerful new architecture of IIS 6.0 relies on techniques such as recycling of worker processes for application reliability and availability. This webcast explains recycling and focuses on some of its potential pitfalls. Learn how to configure the ASP.NET session state service to maintain uptime while minimizing interruptions of application availability.

I seem to send the same code out that I swiped from a program manager and tester (Thanks Alexis and Bruno) here at Microsoft so I thought, hey, why not post it.  This simple code if copied onto a IIS server will allow a user to recycle an application pool.  To do so, you will simply need to paste this into a text file and save it as RecycleAppPool.js.  To use it, simply type in the following:

RecycleAppPool.js <AppPoolNameString>

For example, to recycle the DefaultAppPool that is configured on Windows Server 2003 by default, type the following -

RecycleAppPool.js DefaultAppPool

// get app pool name

cmdArgs = WScript.Arguments;

 

var appPoolName = cmdArgs(0);

WScript.Echo("This application pool will be recycled: " + appPoolName);

 

// instantiate the IIS WMI provider

var service = GetObject( "winmgmts:/root/MicrosoftIISv2" );

 

var appPools = service.ExecQuery(

   "SELECT * FROM IIsApplicationPool " +

   " WHERE Name LIKE '%/" + appPoolName + "' "

);

 

var appPoolEnum = new Enumerator( appPools );

for( ; ! appPoolEnum.atEnd(); appPoolEnum.moveNext() )

{

   appPoolEnum.item().Recycle;           

}

After first releasing this, someone asked me if I might know of a mechanism to do this using WMI and managed code.  After the help of Alexis, I have located this sample which should allow you to do the very same  -

Instructions:

  1. Save this to a file called recycle.cs
  2. In the directory where this file is located, use the following command to compile as a .exe

%windir%\Microsoft.NET\Framework\v1.1.4322\csc.exe /r:System.dll /r:System.Management.dll /t:exe Recycle.cs

Now, type recycle.exe.  Note, this is a very simple example that does not accept parameters like the example in classic JS script.  Thus, it should require some minor modifications.

using System;

using System.Management;

 namespace RecycleAppPool

{

   class RecycleAppPool

   {

     static void Main(string[] args)

      {

 

   ManagementScope scope = new ManagementScope("root\\MicrosoftIISv2");

         scope.Connect();

 

         ManagementObject appPool = new ManagementObject(scope, new ManagementPath("IIsApplicationPool.Name='W3SVC/AppPools/DefaultAppPool'"), null);

         appPool.InvokeMethod("Recycle", null, null);

 

         Console.WriteLine("Recycled DefaultAppPool")

      }

   }

}

I hope that you find this useful and you are welcome to extend the code as you see fit to work in your organizations. 

If you are interested in finding the exact application pool string name that is running currently on your systems, use the built-in application "iisapp.vbs" from the command line.  It should return something like the following:

 


D:\WINDOWS>iisapp
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

W3WP.exe PID: 596   AppPoolId: DefaultAppPool

 

 


If you are one of those who has bit-the-bullet and put 2K3 Service Pack 1 on your machine, you will be happy to know we updated iisapp to now include recycling functionality.  Hence, you don't need that script listed above...seriously, give it a shot!
 

 


D:\WINDOWS>iisapp /p 596 /r
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Application pool 'DefaultAppPool' recycled successfully.

 

D:\WINDOWS>iisapp /a DefaultAppPool /r
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Application pool 'DefaultAppPool' recycled successfully.

 ~Chris

 

Comments

So, how to Stop/Start an Application Pool (not recycle, but the other two options that are available from the MMC)?

Jul 26 2007 by Moshe Eshel

I tried this code sample, but get this error:

System.Management.ManagementException: Provider load failure at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)

I get this error even if i make the account that's running a local administrator on the box...

Any suggestions?

Nov 05 2007 by bigcompanydeveloper

Chris, I have kind of a twist on this:  do you know of a good way to run custom code *after* an app pool has been recycled?  Basically, we have a scenario where some controls are leaking, and the fix isn't out yet, so we're recycling based on memory usage.  After the recycle we want to run a warmup script to get SharePoint all compiled and cached again, but there doesn't seem to be a way to trigger it.  Any ideas?

Thanks for a good post, and for any other ideas you might have.

May 07 2008 by Peter Brunone

Peter, a very rudimentary one-line method could be to add a call to open the web site in your default browser directly after the Recycle, though a delay might be required to give the recycle time to process (?). The browser will make a request to the server which will in turn start up the app.

System.Diagnostics.Process.Start("mywebserver/.../");

Sep 19 2008 by Anonymous