Please add documentation for running Logstash as a Windows service using nssm until Logstash is able to run as a Windows service. See #3721.
Basically:
I highly recommend to have nssm start a separate batch file instead of calling to Logstash.bat directly.
I have my folder structure like this
\logstash\logstash-instance\
\logstash\logstash-instance\logstash.bat
\logstash\logstash-instance\logstash.log
\logstash\logstash-instance\nssm.exe
\logstash\logstash-instance\config\
\logstash\logstash-instance\config\config.conf
\logstash\logstash-instance\java\
\logstash\logstash-instance\java\jdk1.8.0_66\
\logstash\logstash-instance\logstash\
\logstash\logstash-instance\logstash\logstash-2.1.1\
Nssm.exe needs to be somewhere and its good to have it in some specific place because the service it creates calls nssm.exe where you ran it. So, don't run it from your desktop.
Have nssm start logstash.bat in that root in the service.
Logstash.bat is for example like this
set JAVA_HOME=E:\Logstash\Logstash-Instance\java\jdk1.8.0_66
logstash\logstash-2.1.1\bin\logstash.bat agent --config config --log logstash.log
It starts by setting the JAVA_HOME environmental variable where you have the JDK located. Want to upgrade it, just download a new standalone package and extract into the java folder with a new version name, modify the logstash.bat file and restart the service. If it fails you can fall back to the old JDK version the same way.
The second line starts logstash like normally, read all config files in the config folder and log to logstash.log. Be warned that Logstash does not handle log rotates so this file can grow large if something goes wrong. Upgrading Logstash is the same as with JDK, extract to correct folder and modify logstash.bat.
With this you can have many instances of Logstash easily running on the same machine, using different versions of everything and even different Java mem settings, just add those environmental vars to the logstash.bat file.
Scripting the service install with nssm is also doable. Here is a snipped I have for a different use with nssm that runs a powershell script as a service. It expects a certain path and service name but it can easily be modified to make it prompt for the service name or use a part of the path and also get the path to the executable from the current path where the script is located.
Start-Process -FilePath .\nssm.exe -ArgumentList 'install SomeServiceName "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-command "& { . C:\path\to\powershellscript.ps1; Start-Function-Loop }"" ' -NoNewWindow -Wait
Get-Service -Name SomeServiceName
Start-Service -Name SomeServiceName
Get-Service -Name SomeServiceName
And here we also have a good example of why having version names hard coded is a bad thing (...\v1.0\powershell.exe)
@elvarb I am getting a strange issue. I took your suggestion and added the logstash command with required parameters in a batch file. When I run the batch file manually with parameter its running like a charm but when i run it with nssm the service goes to Paused state right away. Any Idea about this issue?
Have the first line CD to the path of logstash folder. It's probably trying to start from c:/windows/system32
@elvarb Thank you for your response. I was providing absolute with also AppDirectory set correctly while also tried your suggestion changing directory but still facing the same issue. I was using the old version 2.1.1, I just updated to later version and it started working.
Glad to see it working
@karenzone This is an old issue, so some of the details have likely changed, but the requirement to document this is still there.
well
Most helpful comment
I highly recommend to have nssm start a separate batch file instead of calling to Logstash.bat directly.
I have my folder structure like this
Nssm.exe needs to be somewhere and its good to have it in some specific place because the service it creates calls nssm.exe where you ran it. So, don't run it from your desktop.
Have nssm start logstash.bat in that root in the service.
Logstash.bat is for example like this
It starts by setting the JAVA_HOME environmental variable where you have the JDK located. Want to upgrade it, just download a new standalone package and extract into the java folder with a new version name, modify the logstash.bat file and restart the service. If it fails you can fall back to the old JDK version the same way.
The second line starts logstash like normally, read all config files in the config folder and log to logstash.log. Be warned that Logstash does not handle log rotates so this file can grow large if something goes wrong. Upgrading Logstash is the same as with JDK, extract to correct folder and modify logstash.bat.
With this you can have many instances of Logstash easily running on the same machine, using different versions of everything and even different Java mem settings, just add those environmental vars to the logstash.bat file.
Scripting the service install with nssm is also doable. Here is a snipped I have for a different use with nssm that runs a powershell script as a service. It expects a certain path and service name but it can easily be modified to make it prompt for the service name or use a part of the path and also get the path to the executable from the current path where the script is located.
And here we also have a good example of why having version names hard coded is a bad thing (...\v1.0\powershell.exe)