Automatically restart Microsoft IIS if website is not available

To automatically restart the Microsoft Internet Information Services (IIS) web server if a website is not available, you need:

  • Administrative access to your web server.
  • The WGET command line utility.
  • A text editor like Notepad to create a batch script file.
  • the Task Scheduler to make the batch script run every n Minutes (e.g. every 10 Minutes)

The batch file I created for one of my own web servers looks like:

@REM ==============================================
@REM Automatically restart IIS if website is not available.
@REM (Checks for a sub string in a page of the website).
@REM
@REM Created 2011-10-28, Uwe Keim uk@zeta.li
@REM ==============================================

@REM Remove any existing previous downloads.
del d:\scripts\index.html

@REM Change drive and folder so that WGET stored in a
@REM well-defined location.
D:
cd d:\scripts

@REM Download file.
D:\scripts\wget.exe ^
    --timeout=30 ^
    --tries=1 ^
    http://www.my-server.com/index.html 

@REM Search for the term in the previously downloaded file.
find /I /C "Some String On Website" d:\scripts\index.html

@REM Restart IIS if string is not found.
IF ERRORLEVEL 1 iisreset /RESTART /TIMEOUT:120 /REBOOTONERROR

Please note the following:

  • WGET downloads the URL to a file with the same file name („index.html“ in my above example)
  • The scheduled task must be created to run with an administrative user.
  • The scheduled task must have the „Run with highest privileges“ checkbox set, because IISRESET only runs with administrative privileges.

Using UrlRewriter.NET in IIS7

When migrating an IIS6 website to IIS7 you probably want to keep an existing UrlRewriter.NET configuration and migrate to the official Microsoft URL Rewriter Module for IIS 7 later on.

Here is the important excerpt from „Web.config“:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="UrlRewriter"
            type="Intelligencia.UrlRewriter.RewriterHttpModule" />
    </modules>
</system.webServer>

Thanks to Rainer who figured this out from this posting.

Another good discussion can be found in the article „Wildcard script mapping and IIS 7 integrated pipeline“ on the IIS website and the article „Use a Single Web.Config for IIS6 and IIS7„.