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.