Keep Your Sitecore Instances Under Control

Learn how easy it is to start/stop your local Sitecore instances with a modified SIF developer script.

Keep Your Sitecore Instances Under Control

If you're a Sitecore developer, chances are that you're running several instances of Sitecore on your developer machine. Some of them are used quite often, as they serve your current project and some of them are just eating up precious resources in the background. So you may think it would be nice to spare the resources they consume and shut them down in an easy way? I've got you covered!

Most probably you use SIF to install a new Sitecore instance on your local machine. Wouldn't it be nice to make re-use of the very same configurations instead of having to copy once more all the names of different Websites and Windows Services together?

Extend your SIF Script

Nowadays Sitecore delivers a SIF script called "XP0-SingleDeveloper.ps1" for each new Sitecore version. This is usually where you start from and adapt the parameters for your local development/playground instance. With some enhancements to your SIF script, you can get yourself an easy setup for stopping/starting any Sitecore XP environment with ease.

Add some params

Starting off from a plain XP0-SingleDelveloper.ps1, simply add some params on the very top of  the script:

Param (    
   [Switch] $StartInstance,    
   [Switch] $StopInstance
)

Do the magic

Replace the SIF commandlet call "Install-SitecoreConfiguration" with the following lines:

$switchStateStartParams = $singleDeveloperParams + @{Tasks = @(
    "IdentityServer_StartWebsite", "IdentityServer_StartAppPool",
    "XConnectXP0_StartWebsite", "XConnectXP0_StartAppPool",
    "XConnectXP0_StartServices", "SitecoreXP0_StartWebsite",
    "SitecoreXP0_StartAppPool")}
$switchStateStopParams = $singleDeveloperParams + @{Tasks = @(
    "IdentityServer_StopWebsite", "IdentityServer_StopAppPool",
    "XConnectXP0_StopServices", "XConnectXP0_StopWebsite",
    "XConnectXP0_StopAppPool","SitecoreXP0_StopWebsite",
    "SitecoreXP0_StopAppPool")}

if ($StartInstance) {
    Install-SitecoreConfiguration @switchStateStartParams *>&1 | Tee-Object XP0-SingleDeveloper-StartInstance.log
}
elseif ($StopInstance) {
    Install-SitecoreConfiguration @switchStateStopParams *>&1 | Tee-Object XP0-SingleDeveloper-StopInstance.log
}
else {
    Install-SitecoreConfiguration @singleDeveloperParams *>&1 | Tee-Object XP0-SingleDeveloper.log
}

So basically you replace the installation commandlet with a switch depending on your above added parameters. In order to just stop/start your instance, there is an enhancement of the already configured SIF-parameter hashtable to only execute some specific SIF tasks.

Usage

Now run the script whenever this particular Sitecore instance should be stopped/started. I recommend adding them somewhere close to the project repository. By providing the Switch parameters, you can control what happens:

.\XP0-SingleDeveloper.ps1 -StartInstance

- starts your Sitecore instance.

.\XP0-SingleDeveloper.ps1 -StopInstance

- stops your Sitecore instance.

.\XP0-SingleDeveloper.ps1

- executes the normal installation process. (Something you most probably don't want too often, so remember the switch-parameters.)

These examples are tested with a Sitecore 9.3 instance. YMMV for other versions.

Additional thoughts

This covers all resources consumed by the IIS Sites, their application pools and the Windows services. As I usually run my Solr instances inside docker containers, its just as easy to start/stop them as needed. So just add an additional script wrapping the SIF commandlets described in this blog post and include some docker compose commandlets. Database memory resources are not covered yet, but I'm usually running all projects from the same local DB instance.

It's a simple and small but still very important step on the road to gain full control over Sitecore development resources.