Let's Share

My personal place to share knowledge about Sitecore, Powershell, Amazon Web Services and .NET

How to install Solr as a Windows Service without dependencies

posted by Robert Senktas   | 14-11-2017

Solr-As-A-ServiceI was always annoyed by the need to use external applications to run Solr as a Windows service. Some time ago I found a PowerShell script that allows you to write services for Windows in PowerShell. I decided to adopt it to run Solr as a Windows service. I cleaned up the code with all the unnecessary functions. I added the following elements in regions where name starts from Solr:

  • #region Solr Configuration
  • #region Solr Start
  • #region Solr Stop
The full script is available on my gist : https://gist.github.com/RobsonAutomator/6a74c6dfceb6bdcde9c27be9f2d36256

Configuration

Configuration is a place where we can specify the port on which Solr will work and the amount of memory used by Java.
The SOLR_HOME variable is used to locate the location where Solr is installed. If it is not set, the script will exit displaying the message: Solr.cmd not exist:

#region Solr configuration
$solrPort = '8983' 
$solrMemory = '512m' # eg. 1g 4g
$solrHome = [environment]::GetEnvironmentVariable("SOLR_HOME",[EnvironmentVariableTarget]::Machine)  
$solrRoot =  Split-Path -Path (Split-Path -Path $solrHome -Parent) -Parent
$sorlStartCmd = Join-Path -Path $solrRoot -ChildPath "bin\solr.cmd"

if( -not (Test-Path -Path $sorlStartCmd) )
{
    Write-Warning "Solr.cmd not exist: $sorlStartCmd"
    return
}
#endregion


Start

In the region 'Solr Start' there is a code to run Solr:

#region Solr start
Log "$scriptName Starting $sorlStartCmd with parameteres start -f -p $solrPort -m $solrMemory"
&$sorlStartCmd start -f -p $solrPort -m $solrMemory
#endregion


Stop

In the region "Solr Stop" there is a code to stop Solr:

#region Solr stop 
&$sorlStartCmd stop -p $solrPort
#endregion


Setup

First of all You have to run script with administrator rights
Use this script is extremally simple. First run script with parameter -Setup
This will create PSSolrService.exe and PSSolrService.ps1 in the folder:

$installDir = "${ENV:ProgramFiles}\$serviceName"
When PSSolrService is installed then use -Start parameter to start service. Till now you can start/stop PSSolrService as each Windows service.


Sitecore Solr Windows Service Powershell
This is a personal blog. All opinions here are my own opinions and do not represent my employer’s view in anyway.

Sitecore Automation Module