I 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:
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
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
In the region "Solr Stop" there is a code to stop Solr:
#region Solr stop &$sorlStartCmd stop -p $solrPort #endregion
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.