Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to run JBoss as a service on Windows platforms.
@ECHO off
::-------------------------------------------------------------------
:: Configuration: before running you need to configure these
:: This script works with JBoss 5.1 and 6.1
:: *This is my first try at this script. I may rewrite this
:: in a more organized way.*
::-------------------------------------------------------------------
::-------------------------------------------------------------------
:: Configuration: before running you need to configure these::
::-------------------------------------------------------------------
SET VER=JBOSS51
SET DEBUGLEVEL=DEBUG
SET DELAY=10
SET INSTANCE=default
:: NEED to require a file here: %JBOSS_HOME%\server\ports-bindings.xml
:: Also, enable org.jboss.services.binding.ServiceBindingManager in
:: server\%INSTANCE%\conf\jboss-service.xml and point it to the ports-bindings.xml
:: file using <attribute name="StoreURL">${jboss.home.url}/server/ports-bindings.xml</attribute>
::-------------------------------------------------------------------
:: Begin script. No need to edit below this line.
::-------------------------------------------------------------------
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
CLS
:: FOR /R . %%F IN (*.sh) DO DEL /F "%%F"
SET PICK=
:: place this next to the run.bat in your JBoss distro and run it
IF EXIST C:\Users (
ECHO Current User is '%USERNAME%'
ECHO INFO: You may need to run this script as 'Administrator' user, depending
ECHO on what your system configuration looks like.
)
IF NOT EXIST wrapper\lib (
ECHO.
ECHO The YAJSW distributions 'lib' directory is needed in a directory called .\wrapper
ECHO that is relative to this script and the run.bat .
ECHO Under .\wrapper you also need the wrapper.jar and wrapper.jboss.conf .
ECHO Download distro from http://sourceforge.net/projects/yajsw/ .
C:
START "%ProgramFiles%\Internet Explorer\iexplore.exe" http://sourceforge.net/projects/yajsw
ECHO.
CALL :waitseconds 10
GOTO :END
)
::-------------------------------------------------------------------
:: Set TITLE, JBOSS_HOME, and DIRNAME variables
::-------------------------------------------------------------------
SET JBOSS_HOME=%~dp0
FOR /F "delims=\" %%a IN ("%JBOSS_HOME%") DO SET "DIRNAME=%%~nxa"
SET "APPDIR=%DIRNAME%"
SET "JBOSS_HOME=%JBOSS_HOME:bin\=%"
IF %JBOSS_HOME:~-1%==\ SET "JBOSS_HOME=%JBOSS_HOME:~0,-1%"
::-------------------------------------------------------------------
:: Parse first script argument for service action
:: If no args are passed then prompt user
::-------------------------------------------------------------------
:MAKECHOICE
IF "%1"=="" SET PICK=%~1
IF "%2"=="" SET SERVICENAME=%~2
ECHO.&ECHO.&ECHO.
IF "%PICK%"=="" (
ECHO -----------------------------
ECHO Options menu:
ECHO Script also accepts args without prompting: script auto [servicename], or script [action]
ECHO -----------------------------
ECHO [auto] Install and start service
ECHO [install] Install service
CALL :countlines services.txt
IF !THECOUNT! GTR 0 (
ECHO [start] Start service
ECHO [stop] Stop service
ECHO [remove] Remove or uninstall service
ECHO [console] Run service as console
ECHO [status] Query service status
)
ECHO [X] Exit script
ECHO.
) ELSE (
ECHO Running silently since script has arguments.
)
IF "%PICK%"=="" SET /P "PICK=Please enter a service action you want to perform [auto]: "
IF "%PICK%"=="" (
CLS&ECHO.&ECHO.
ECHO You must select an action. Try again.
ECHO.
GOTO :MAKECHOICE
)
IF "%PICK%"=="auto" (
IF "%2"=="" CALL :setname
SET SERVICENAME=!NICKNAME!
GOTO :ACTIONSET
)
IF "%PICK%"=="install" (
IF NOT "%2"=="" (
SET SERVICENAME=%2
) ELSE (
CALL :setname
SET SERVICENAME=!NICKNAME!
)
SET ACTION=Installing
SET OPTION=-i
GOTO :ACTIONSET
)
CALL :countlines services.txt
IF !THECOUNT! GTR 0 (
IF "%PICK%"=="start" (
IF "%2"=="" (
CALL :promptwithlist start
SET SERVICENAME=!NICKNAME!
) ELSE (
SET SERVICENAME=%2
)
CALL :verifylist !SERVICENAME!
SET ACTION=Starting
SET OPTION=-t
GOTO :ACTIONSET
)
IF "%PICK%"=="stop" (
IF "%2"=="" (
CALL :promptwithlist stop
SET SERVICENAME=!NICKNAME!
) ELSE (
SET SERVICENAME=%2
)
CALL :verifylist !SERVICENAME!
SET ACTION=Stopping
SET OPTION=-p
GOTO :ACTIONSET
)
IF "%PICK%"=="remove" (
IF "%2"=="" (
CALL :promptwithlist remove
SET SERVICENAME=!NICKNAME!
) ELSE (
SET SERVICENAME=%2
)
CALL :verifylist !SERVICENAME!
SET ACTION=Removing
SET OPTION=-r
GOTO :ACTIONSET
)
IF "%PICK%"=="console" (
IF "%2"=="" (
CALL :promptwithlist "run as console"
SET SERVICENAME=!NICKNAME!
) ELSE (
SET SERVICENAME=%2
)
CALL :verifylist !SERVICENAME!
SET ACTION=Console
SET OPTION=-c
GOTO :ACTIONSET
)
IF "%PICK%"=="status" (
IF "%2"=="" (
CALL :promptwithlist "query status"
SET SERVICENAME=!NICKNAME!
) ELSE (
SET SERVICENAME=%2
)
CALL :verifylist !SERVICENAME!
SET ACTION=Querying
SET OPTION=-q
GOTO :ACTIONSET
)
)
IF "%PICK%"=="X" GOTO :EOF
IF "%PICK%"=="x" GOTO :EOF
:ACTIONSET
CALL :settitle
SET "WRAPPER_CONFIG_FILE=%JBOSS_HOME%\%APPDIR%\wrapper\wrapper.%VER%.conf"
::-------------------------------------------------------------------
:: Set java options for wrapper
:: Change back-slashes to forward-slashes before passing options to the wrapper
::-------------------------------------------------------------------
SET "WRAPPER_ARGS=-Djboss.home=%JBOSS_HOME% -Djboss.dir=%APPDIR% -Dservice.name=%SERVICENAME% -Ddebug.level=%DEBUGLEVEL% -Djboss.inst=%INSTANCE%"
SET WRAPPER_ARGS=%WRAPPER_ARGS:\=/%
::-------------------------------------------------------------------
:: Info output
::-------------------------------------------------------------------
ECHO.
ECHO -- These variables are set by this script and passed to the service installation:
ECHO JAVA_HOME=%JAVA_HOME%
ECHO JBOSS_HOME=%JBOSS_HOME%
ECHO APPDIR=%APPDIR%
ECHO WRAPPER_CONFIG_FILE: %WRAPPER_CONFIG_FILE%
ECHO WRAPPER_ARGS=%WRAPPER_ARGS%
ECHO SERVICENAME=%SERVICENAME%
ECHO ACTION=%ACTION%
ECHO OPTION=%OPTION%
ECHO.
::ECHO -- Before you install this service make sure that you properly configured the
::ECHO property called "wrapper.ntservice.account" in the wrapper config file.
::ECHO (It will default to running as the "Local System" user.)
ECHO.
::-------------------------------------------------------------------
:: Install service
::-------------------------------------------------------------------
ECHO.
IF "%PICK%"=="auto" (
:: with pick of "auto", perform 2 actions
ECHO.
ECHO Registering service ...
SET OPTION=-i
ECHO "%JAVA_HOME%\bin\java.exe" %WRAPPER_ARGS% -jar %JBOSS_HOME%\%APPDIR%\wrapper\wrapper.jar -i %WRAPPER_CONFIG_FILE%
CALL "%JAVA_HOME%\bin\java.exe" %WRAPPER_ARGS% -jar %JBOSS_HOME%\%APPDIR%\wrapper\wrapper.jar -i %WRAPPER_CONFIG_FILE%
CALL :waitseconds 3
ECHO Starting service ...
ECHO "%JAVA_HOME%\bin\java.exe" %WRAPPER_ARGS% -jar %JBOSS_HOME%\%APPDIR%\wrapper\wrapper.jar -t %WRAPPER_CONFIG_FILE%
CALL "%JAVA_HOME%\bin\java.exe" %WRAPPER_ARGS% -jar %JBOSS_HOME%\%APPDIR%\wrapper\wrapper.jar -t %WRAPPER_CONFIG_FILE%
ECHO.
ECHO Registering and starting of service ^"%SERVICENAME%^" is finished.
) ELSE (
ECHO.
ECHO %ACTION% service ...
ECHO "%JAVA_HOME%\bin\java.exe" %WRAPPER_ARGS% -jar %JBOSS_HOME%\%APPDIR%\wrapper\wrapper.jar %OPTION% %WRAPPER_CONFIG_FILE%
CALL "%JAVA_HOME%\bin\java.exe" %WRAPPER_ARGS% -jar %JBOSS_HOME%\%APPDIR%\wrapper\wrapper.jar %OPTION% %WRAPPER_CONFIG_FILE%
ECHO.
ECHO %ACTION% service ^"%SERVICENAME%^" is finished.
)
ECHO.
::-------------------------------------------------------------------
:: Update services.txt file list
::-------------------------------------------------------------------
IF "%OPTION%"=="-i" (
CALL :addtolist %SERVICENAME%
CALL :setstartup
)
IF "%OPTION%"=="-r" CALL :removefromlist %SERVICENAME%
IF "%PICK%"=="query" GOTO :MAKECHOICE
IF "%PICK%"=="console" GOTO :MAKECHOICE
IF "%PICK%"=="remove" GOTO :MAKECHOICE
GOTO :END
::-------------------------------------------------------------------
:: Functions
::-------------------------------------------------------------------
:waitseconds timeArg
SET /A VAL=%1
ECHO Pausing for %VAL% seconds.
SET /A DUR=%VAL%*2
PING.exe -n %DUR% -w 1 127.0.0.1>nul
EXIT /B 0
:verifylist arg1
ECHO Function: verifylist
:: verify that arg1 is contained in services.txt file list
IF "%1"=="" CALL :error "Error with verifylist." "Missing arg."
SET FOUND=
FOR /F "tokens=1 delims=^=" %%I in (services.txt) DO (
IF "%1"=="%%I" (
SET FOUND=1
)
)
IF NOT "%FOUND%"=="1" (
ECHO Invalid service name ^"%1^". Try again.
ECHO.
GOTO :MAKECHOICE
) ELSE (
ECHO Service name ^"%1^" is a valid registered name.
)
EXIT /B 0
:promptwithlist arg1
ECHO ---------------------------
ECHO List of services
ECHO.
CALL :countlines services.txt
IF %THECOUNT% GTR 1 (
FOR /F "tokens=1 delims=^=" %%M IN (services.txt) DO ECHO %%M
ECHO.
SET /P "NICKNAME=Enter the name of one of these services to %1: "
)
IF %THECOUNT%==1 SET /P NICKNAME=<services.txt
FOR /F "tokens=1 delims=^=" %%M IN (%NICKNAME%) DO SET NICKNAME=%%M
IF %THECOUNT%==0 ECHO No existing services to choose from. Try again.&GOTO :MAKECHOICE
EXIT /B 0
:addtolist arg1
ECHO Adding service ^"%1^" to list.
IF "%1"=="" CALL :error "Error with removefromlist." "Missing argument."
FOR /F %%I in (services.txt) DO (
IF NOT "%1"=="%%I" ECHO %%I>>services.tmp
)
ECHO %1=%INSTANCE%>>services.tmp
COPY /Y services.tmp services.txt
DEL /Q services.tmp
EXIT /B
:removefromlist arg1
ECHO Removing service ^"%1^" from list.
IF "%1"=="" CALL :error "Error with removefromlist." "Missing argument."
ECHO. 2>services.tmp
FOR /F "delims=^= tokens=1" %%I in (services.txt) DO (
IF NOT "%1"=="%%I" ECHO %%I>>services.tmp
)
COPY /Y services.tmp services.txt
DEL /Q services.tmp
EXIT /B
:setname
SET NICKNAME=DEFAULT
SET /P "NICKNAME=Enter a name for your service [JBOSS]: "
IF "%NICKNAME%"=="" SET SERVICENAME=JBOSS
CALL :settitle
EXIT /B 0
:settitle
SET TITLETEXT=JBoss ^"!SERVICENAME!^" Service Controller
TITLE %TITLETEXT%
EXIT /B 0
:setstartup arg
:: arg is either <boot|system|auto|demand|disabled|delayed-auto>
IF "%1"=="" (
SET STARTUP=
ECHO.&ECHO.
ECHO --------------------------
ECHO Service can be registered to start in different ways:
ECHO boot,system,auto,demand,disabled, or delayed-auto
ECHO --------------------------
SET /P "STARTUP=Choose a service startup type: "
) ELSE (
SET STARTUP=%1
)
IF "%PICK%"=="auto" SET STARTUP=auto
ECHO.
sc.exe config %SERVICENAME% start= %STARTUP%
EXIT /B 0
:countlines arg1 arg2
:: args are filename and string-to-match
SET MATCH=
IF "%2"=="" (
SET MATCH=JBOSS
) ELSE (
SET MATCH=%2
)
FINDSTR /R /N "^" %1 | find /C "%MATCH%" >out.tmp
SET /P THECOUNT=<out.tmp
DEL /Q out.tmp
EXIT /B 0
:error String1 String2 String3
ECHO Error: %~1
IF NOT "%~2"=="" ECHO %~2
IF NOT "%~3"=="" ECHO %~3
ECHO.
PAUSE
GOTO :END
::-------------------------------------------------------------------
:: End script
::-------------------------------------------------------------------
:END
ECHO.
ECHO Service script is finished and will close automatically in %DELAY% seconds.
FOR /l %%a IN (%DELAY%,-1,1) DO (TITLE JBoss %APPDIR% -- Closing in %%as&PING.exe -n 2 -w 1 127.0.0.1>nul)
# wrapper.JBOSS51.conf
wrapper.working.dir=${jboss.home}/bin
wrapper.java.app.mainclass=org.jboss.Main
wrapper.java.classpath.1=run.jar
wrapper.console.loglevel=${debug.level}
wrapper.debug
# these are the JAVA_OPTS
# enviroment variables - define the ones that match your desired environment
wrapper.java.additional.1=-Dprogram.name=run.bat
wrapper.java.additional.2=-Xms1024M
wrapper.java.additional.3=-Xmx1523M
wrapper.java.additional.4=-XX:MaxPermSize=256m
wrapper.java.additional.5=-Dsun.rmi.dgc.client.gcInterval=3600000
wrapper.java.additional.6=-Dsun.rmi.dgc.server.gcInterval=3600000
wrapper.java.additional.7=-Dorg.jboss.resolver.warning=true
wrapper.java.additional.8=-Djava.endorsed.dirs=${jboss.home}/lib/endorsed
wrapper.java.additional.9=-Dsun.lang.ClassLoader.allowArraySyntax=true
wrapper.java.additional.10=-Djboss.service.binding.set=${instance.name}
# wrapper.java.additional.11=-Djava.awt.headless=true
# wrapper.java.additional.12=-Xrunjdwp:transport=dt_shmem,address=jboss,server=y,suspend=n
# wrapper.java.additional.134=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n
# If you need serialization support
wrapper.java.additional.24=-Dsession.serialization.jboss=true
# Parameters to be passed to the JBoss application
wrapper.app.parameter.1=org.jboss.Main
# Define server instance name to control
wrapper.app.parameter.2=-c
wrapper.app.parameter.3=${jboss.inst}
# Define listening IP - If you have more than one IP or want to indicate to listen on a specific IP
wrapper.app.parameter.4=-b
wrapper.app.parameter.5=0.0.0.0
# wrapper log location
wrapper.logfile=${jboss.home}/bin/${service.name}.log
wrapper.console.title=JBOSS Service Installation, Maintenance and Debugging
wrapper.ntservice.name=${service.name}
wrapper.ntservice.displayname=${service.name}
wrapper.ntservice.description=JBOSS Wrapped Service/Application Server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment