Keeping IIS Sites Alive With PowerShell and Task Scheduler



Last updated: January 28th, 2024

Problem: Sites Go Inactive

On IIS, sites that aren't in constant use eventually go inactive. As a result, a first request to a website when it's inactive can take a considerable amount of time. This negatively impacts user experience.

IIS Settings

On IIS, the first visit to an Application Pool needs to create a new w3wp.exe process. This is slow because the App Pool needs to be created.

To deal with this, one option is to go into IIS Manager, then to Application Pools and change the setting "Idle Time-out (minutes)" from 20 to 0. This configuration dialog is seen by selecting "Advanced Settings..." from the menu.

More details on IIS App Pools here.

Using A Ping Service

Even if a site doesn't time out from inactivity, it may have never loaded in the first place due to server restart.

A site can be in a ready state if it's pinged frequently. One of way to do this is to create a task in Windows Task Scheduler that will hit sites every few minutes to make sure they aren't idle. You can list explict URLs to hit.

Another option, if you have some kind of naming convention for your IIS sites which allows it, is to loop through all the sites. This is what I decided to do below. I get all the sites which are named as something like whatever.com and turn this into a URL, request the URL and ignore the response:

Setup Task Scheduler

Open Task Scheduler (%windir%\system32\taskschd.msc /s).

In the program:

  • Click to Create Basic Task
  • Give the task a name and description
  • Select the task to start Daily. Select to Start a program.

Details on Action:

  • Action should show as: "Start a program"
  • The Program/script should be: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" - to execute PowerShell
  • Under Add arguments (optional) put the location to your PowerShell script, e.g.: "C:\my_scripts\keepalive.ps1"

Modify task frequency:

  • On the created task > right click > properties > Triggers -> Edit > Advance Settings > select "Repeat task every" X minutes.
  • Specify how fequently you want the task to run. 10 minutes is frequent enough.

Run in headless/ background mode:

  • Select the task and go to Properties
  • Click the "Change User or Group..." button
  • Type in "SYSTEM" and press OK

Soution: Keeping Sites In A Ready State

By either keeping Application Pools from timing out after their first request to using a custom ping service with Windows Task Manager, it is possible to keep sites in a ready state for users.

It may be best to utilize both methods of keeping App Pools alive and starting a process every few minutes to make sure they are active. For some sites, they might want certain, many or all of their pages to be in a cache as well. This could be achieved by having a more advanced ping service and caching policy for a certain use case.

Either way, utilizing one or both of these methods can help with the user experience of sites on IIS, even if they get low traffic volume.


Comments

No Comments

Post Comment

Prove you are human 2 + 6 =

Tagged: IIS, PowerShell


Join my email list!



ryan
About Me

With 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up With SoftSys Hosting! (My host)