Sling as windows service

How to install Apache Sling as a windows service

Let's have:
- C:\sling direcotry
- Apache Commons Daemon procrun - http://commons.apache.org/proper/commons-daemon/procrun.html
- be aware to use amd64 version for 64 bit windows
- update and run following bat file:

installService.bat :

@echo off
set SERVICE_NAME=SlingService
set SERVICE_DESCRIPTION=E-learning
set SLING_HOME=C:\Users\initedsol\vyuka
set SLING_DATA=C:\Users\initedsol\vyuka\sling
set SLING_JAR=org.apache.sling.launchpad-9-SNAPSHOT.jar
set SLING_PORT=8080
set PR_INSTALL=C:\Users\initedsol\vyuka\prunsrv.exe

@REM Service Log Configuration
set PR_LOGPREFIX=%SERVICE_NAME%
set PR_LOGPATH=%SLING_HOME%\logs
set PR_STDOUTPUT=auto
set PR_STDERROR=auto
set PR_LOGLEVEL=Debug

@REM Path to Java Installation
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_162
set PR_JVM=%JAVA_HOME%\jre\bin\server\jvm.dll
set PR_CLASSPATH=%SLING_HOME%\%SLING_JAR%;%JAVA_HOME%\lib\tools.jar

@REM JVM Configuration
set PR_JVMMS=128
set PR_JVMMX=1536
set PR_JVMSS=4000
set PR_JVMOPTIONS=-Dfile.encoding=UTF-8
@REM Startup Configuration
set SLING_START_CLASS=org.apache.sling.launchpad.app.Main

set PR_STARTUP=auto
set PR_STARTMODE=java
set PR_STARTCLASS=%SLING_START_CLASS%
set PR_STARTPARAMS=start#-p#%SLING_PORT%#-c#%SLING_DATA%

@REM Shutdown Configuration
set PR_STOPMODE=java
set PR_STOPCLASS=%SLING_START_CLASS%
set PR_STOPPARAMS=stop#-c#%SLING_DATA%

"%PR_INSTALL%" //IS/%SERVICE_NAME% ^
  --DisplayName="%SERVICE_NAME%" ^
  --Description="%SERVICE_DESCRIPTION%" ^
  --Install="%PR_INSTALL%" ^
  --Startup="%PR_STARTUP%" ^
  --LogPath="%PR_LOGPATH%" ^
  --LogPrefix="%PR_LOGPREFIX%" ^
  --LogLevel="%PR_LOGLEVEL%" ^
  --StdOutput="%PR_STDOUTPUT%" ^
  --StdError="%PR_STDERROR%" ^
  --JavaHome="%JAVA_HOME%" ^
  --Jvm="%PR_JVM%" ^
  --JvmMs="%PR_JVMMS%" ^
  --JvmMx="%PR_JVMMX%" ^
  --JvmSs="%PR_JVMSS%" ^
  --JvmOptions=%PR_JVMOPTIONS% ^
  --Classpath="%PR_CLASSPATH%" ^
  --StartPath="%SLING_HOME%" ^
  --StartMode="%PR_STARTMODE%" ^
  --StartClass="%SLING_START_CLASS%" ^
  --StartParams="%PR_STARTPARAMS%" ^
  --StopMode="%PR_STOPMODE%" ^
  --StopClass="%PR_STOPCLASS%" ^
  --StopParams="%PR_STOPPARAMS%"

if not errorlevel 1 goto installed
echo Failed to install "%SERVICE_NAME%" service.  Refer to log in %PR_LOGPATH%
goto end

:installed
echo The Service "%SERVICE_NAME%" has been installed

:end

To debug registered service execute as Administrator:

prunsrv //TS//SlingService

Registered service stores data on two places in registry:
1. service: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SlingService
2. Apache: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\SlingService\Parameters\Java

Inspiration links:
- https://www.eclipse.org/jetty/documentation/current/startup-windows-service.html
- http://commons.apache.org/proper/commons-daemon/procrun.html

Není-li uvedeno jinak, obsah této stránky je pod licencí Creative Commons Attribution-ShareAlike 3.0 License