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
Post a Comment

Labels

Java (159) Lucene-Solr (112) Interview (61) All (58) J2SE (53) Algorithm (45) Soft Skills (38) Eclipse (33) Code Example (31) Linux (25) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) J2EE (13) Network (13) Troubleshooting (13) PowerShell (11) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) Problem Solving (9) UIMA (9) html (9) Http Client (8) Maven (8) Security (8) bat (8) blogger (8) Big Data (7) Continuous Integration (7) Google (7) Guava (7) JSON (7) Shell (7) ANT (6) Coding Skills (6) Database (6) Lesson Learned (6) Programmer Skills (6) Scala (6) Tips (6) css (6) Algorithm Series (5) Cache (5) Dynamic Languages (5) IDE (5) System Design (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Firefox (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Python (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts