Windows BAT: Using Apache Procrun to Install Java Application As Windows Service


I have a java application which combines embedded jetty and Solr in one application, and wrap it as a windows service.

This embedded jetty and Solr application should be able to be installed at different locations, and run as a different services.

To do this: I use Apache procrun, put prunsrv.exe and prunmgr.exe in bin folder, create several windowshowAdsenses bat files to install it as a widows service, to uninstall it, start and stop the services.
installService.bat
We can install the application with specified service name, service display name, service description, we can use start_params to specify parameters to the application like port number.

The script will first use %PRUNSRV% //IS, %PRUNSRV% //US  to install it as a service, then copy prunmgr.exe to %service_name%.exe, so we can use %service_name%.exe to monitor, edit and manage the service.

installService.bat -service_name service1 -service_display_name display_name1 -service_description service_description1 -start_params "start;-port;9753" -stop_params shutdown

@echo off
@echo off
@setlocal enableextensions enabledelayedexpansion
goto install

:parseParams
set key=%~1
set value=%~2
if "%key%" == "" goto :eof
if "%key%" == "-PR_JAVA_OPTIONS" (   
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -PR_JAVA_OPTIONS
        goto end
    )
 SET PR_JAVA_OPTIONS=%value%
) else if "%key%" == "-remotedebug" (  
 shift
 SET remotedebug=true
) else if "%key%" == "-action" (  
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -action
        goto end
    )
 SET ACTION=%value%
) else if "%key%" == "-service_name" (  
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_name
        goto end
    )
 SET SERVICE_JAVA=%value%
) else if "%key%" == "-service_display_name" (  
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_display_name
        goto end
    )
 SET PR_DESPLAYNAME=%value%
) else if "%key%" == "-service_description" (  
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_description
        goto end
    )
 SET PR_DESCRIPTION=%value%
) else if "%key%" == "-start_params" (  
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -start_params
        goto end
    )
 SET START_PARAMS=%value%
) else if "%key%" == "-stop_params" (  
 shift
 shift
 if "%value%" == "" (
        echo Empty value for -stop_params
        goto end
    )
 SET STOP_PARAMS=%value%
) else if "%key%" == "-pr_jvm" (  
  shift
 shift
 if "%value%" == "" (
        echo Empty value for -pr_jvm
        goto end
    )
 SET PR_JVM=%value%
) else if "%key%" == "-uninstallIfExsit" (  
 shift
 SET UNINSTALL_IF_EXSIT=true
) else (
 shift
)
goto parseParams %*
goto :eof

:install
set SERVICE_JAVA=MyApp
set PR_DESPLAYNAME=MyApp
set PR_DESCRIPTION="MyApp Service"
set START_PARAMS=start
set STOP_PARAMS=shutdown
set PR_JVM=auto
SET PR_JAVA_OPTIONS="-Xms512m;"
SET CMD_OPTS=%*
set APP_RUNNING_HOME=
set "APP_HOME=%~dp0/../"
set "PATH_PRUNSRV=%~dp0"
set "PR_LOGPATH=%APP_HOME%/logs"
set UNINSTALL_IF_EXSIT=false
SET MYCLASSPATH=
rem set PR_INSTALL=%PATH_PRUNSRV%prunsrv.exe

call :parseParams %*
set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe
if "%PR_DESPLAYNAME%" == "" (
 set PR_DESPLAYNAME=%SERVICE_JAVA%
)

REM check whether service already exists
echo Installing %SERVICE_JAVA%
SC QUERY %SERVICE_JAVA% > NUL
REM if ERRORLEVEL 1060 means it doesn't exists
IF not ERRORLEVEL 1060 (
 if "%UNINSTALL_IF_EXSIT%" == "true" (
  "%PRUNSRV%" //DS//%SERVICE_JAVA%
 ) else  (
  echo %SERVICE_JAVA% already exists, and uninstallIfExsit is false. Do nothing
  goto end
 )
)

"%PRUNSRV%" //IS//%SERVICE_JAVA% --Install="%PRUNSRV%"

if not errorlevel 1 goto installed
echo Failed to install '%SERVICE_JAVA%' service
goto end
:installed
echo The service '%SERVICE_JAVA%' has been installed.
set libDir=%APP_HOME%\lib
set MYCLASSPATH="%libDir%\*;%libDir%\ext\*;%libDir%\shared-libs\*;%APP_HOME%\etc"
if "%remotedebug%" == "true" (
 SET PR_JAVA_OPTIONS="%PR_JAVA_OPTIONS%;-agentlib:jdwp=transport=dt_socket,address=9999,server=y,suspend=n"
)

echo Setting the parameters for %SERVICE_JAVA%
echo "%PRUNSRV%" //US//%SERVICE_JAVA% --DisplayName "%PR_DESPLAYNAME%" --Description "%PR_DESCRIPTION%" --StdOutput auto --StdError auto ^
--Classpath=%MYCLASSPATH% --Jvm="%PR_JVM%" --JvmOptions="%PR_JAVA_OPTIONS%" --Startup=auto ^
--StartMode=jvm --StartClass=MainClass --StartParams="%START_PARAMS%" ^
--StopMode=jvm  --StopClass=MainClass  --StopParams="%STOP_PARAMS%"

"%PRUNSRV%" //US//%SERVICE_JAVA% --DisplayName "%PR_DESPLAYNAME%" --Description "%PR_DESCRIPTION%" --StdOutput auto --StdError auto ^
--Classpath=%MYCLASSPATH% --Jvm="%PR_JVM%" --JvmOptions="%PR_JAVA_OPTIONS%" --StartPath %APP_HOME% --Startup=auto ^
--StartMode=jvm --StartClass=MainClass --StartParams="%START_PARAMS%" ^
--StopMode=jvm  --StopClass=MainClass  --StopParams="%STOP_PARAMS%"

if not errorlevel 1 goto updated
echo Failed updating '%SERVICE_JAVA%' service
goto end
:updated

If NOT exist %SERVICE_JAVA%w.exe (
 copy /B prunmgr.exe %SERVICE_JAVA%w.exe
)
echo The service '%SERVICE_JAVA%' has been updated.
echo Installation of %SERVICE_JAVA% is complete
goto end

:end
endlocal 
@echo on
uninstallService.bat
To uninstall the service, we just need specify service name:
uninstallService.bat -service_name service1
It will use "%PRUNSRV%" //DS to uninstall the service.
@echo off
setlocal
goto uninstall

:parseParams
set key=%~1
set value=%~2
if "%key%" == "" goto :eof
if "%key%" == "-service_name" (  
  shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_name
        goto end
    )
 SET SERVICE_JAVA=%value%
) else if "%key%" == "-delete_exe" (  
 shift
 SET DELETE_EXE=true
) else (
 shift
)
goto parseParams %*
goto :eof

:uninstall
set MYPATH=%~dp0
set PATH_PRUNSRV=%MYPATH%
set "PR_LOGPATH=%MYPATH%/../logs"
set SERVICE_JAVA=MyApp
set DELETE_EXE=false

call :parseParams %*
set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe
echo Removing %SERVICE_JAVA%

cmd /c stopService.bat -service_name %SERVICE_JAVA%
"%PRUNSRV%" //DS//%SERVICE_JAVA%

if "%DELETE_EXE%" == "true" (
  del /Q %SERVICE_JAVA%.exe
 echo %SERVICE_JAVA%.exe deleted
)
if not errorlevel 1 goto removed
echo.
echo Failed uninstalling '%SERVICE_JAVA%' service
goto :eof
:removed
echo The service '%SERVICE_JAVA%' has been removed

endlocal
@echo on
startService.bat/stopService.bat
It's very easy to implement: just get service name, then use %PRUNSRV% //ES to start the service, use %PRUNSRV% //SS to stop the service.
startService.bat/stopService.bat -service_name service1
startService.bat
@echo off
setlocal

goto run
:parseParams
set key=%~1
set value=%~2
echo %key% %value%
if "%key%" == "" goto :eof
if "%key%" == "-service_name" (  
  shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_name
        goto end
    )
 SET SERVICE_JAVA=%value%
) else if "%key%" == "-install_if_not_exist" (  
  shift
 SET INSTALL_IF_NOT_EXIST=true
) else if "%key%" == "-service_description" (  
  shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_description
        goto end
    )
 SET service_description=%value%
) else if "%key%" == "-service_display_name" (  
  shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_display_name
        goto end
    )
 SET service_display_name=%value%
) else if "%key%" == "-start_params" (  
  shift
 shift
 if "%value%" == "" (
        echo Empty value for -start_params
        goto end
    )
 SET start_params=%value%
) else (
 shift
)
goto parseParams %*
goto :eof

:run
set "PATH_PRUNSRV=%~dp0"
set SERVICE_JAVA=MyApp
set INSTALL_IF_NOT_EXIST=true
set service_display_name=MyApp
set service_description="MyApp Service"
set start_params=""
call :parseParams %*

set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe
REM if INSTALL_IF_NOT_EXIST is true, check whether the service exists, if false, install the service if needed.
SC QUERY %SERVICE_JAVA% > NUL 2>&1
REM if ERRORLEVEL 1060 means it doesn't exists
IF ERRORLEVEL 1060 (
  echo %SERVICE_JAVA% doesn't exists
 if "%INSTALL_IF_NOT_EXIST%" == "true" (
   echo install_if_not_exist is true, install the service %SERVICE_JAVA%.
  cmd /c installEmbededJettyService.bat -service_name %SERVICE_JAVA% -service_display_name "%service_display_name%" -service_description "MyApp Service" -start_params "%start_params%"
  echo service %SERVICE_JAVA% is installed.
 ) else (
  echo install_if_not_exist is false, exit with error.
  exit -1
 )
)

%PRUNSRV% //ES//%SERVICE_JAVA%
if %ERRORLEVEL% == 0 (
 echo %SERVICE_JAVA% is started.
) else (
 echo Failed starting '%SERVICE_JAVA%' service
)

:end
endlocal 
@echo on
StopService.bat
@echo off
setlocal

goto run
:parseParams
set key=%~1
set value=%~2
echo %key% %value%
if "%key%" == "" goto :eof
if "%key%" == "-service_name" (  
    shift
 shift
 if "%value%" == "" (
        echo Empty value for -service_name
        goto end
    )
 SET SERVICE_JAVA=%value%
) else (
 shift
)
goto parseParams %*
goto :eof

:run

set PATH_PRUNSRV=%~dp0
set SERVICE_JAVA=MyApp
call :parseParams %*

set PRUNSRV=%PATH_PRUNSRV%%SERVICE_JAVA%.exe
"%PRUNSRV%"  //SS//%SERVICE_JAVA%
rem force kill the service as a safety bet
Taskkill /F /FI "SERVICES eq %SERVICE_JAVA%"
rem recheck service exists
Tasklist /FI "SERVICES eq %SERVICE_JAVA%" 2>NUL | find /I /N "%SERVICE_JAVA%">NUL 2>&1
if %ERRORLEVEL% == 0 (
 echo Failed stopping '%SERVICE_JAVA%' service
) else (
  echo %SERVICE_JAVA% is stopped.
)
:end
endlocal 
@echo on
Resource
http://commons.apache.org/proper/commons-daemon/procrun.html
Commons Daemon Procrun as a Java Service Wrapper for ActiveMQ

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)