How to run WildFly as Service

[UPDATED!] This tutorial illustrates how to configure WildFly and JBoss AS as a Service (covering all server releases!) both in Windows environment and in Unix-Linux environment.

WildFly as a Service for Linux users

In order to start the application server as service using a Linux distribution you can use the scripts which are located under the JBOSS_HOME/docs/contrib/scripts/init.d folder. If you look into this folder, you will find the following files:

  • wildfly-­init­-redhat.sh : this file needs to be used for Red Hat Enterprise­-like Linux distributions (e.g. RHEL, Centos)
  • wildfly­-init­-debian.sh: this file needs to be used for Debian­like Linux distributions (e.g.Debian, Ubuntu)
  • wildfly.conf: this file contains the configuration used by the above init files

As first step, copy the shell script, which is required by your Linux distribution into the /etc/init.d folder. For example, if we were to install WildFly as a service on RHEL:

$ cp wildfly-init-redhat.sh /etc/init.d/wildfly

Now we will copy as well the wildfly.conf configuration file in the location where the startup script expects it:

$ mkdir -p /etc/default
$ cp wildfly.conf /etc/default

Within the wildfly.conf file adjust the settings in order to fit your installation:

#Location of Java
JAVA_HOME=/usr/java/jdk1.8.0_45
# Location of WildFly
JBOSS_HOME=/usr/share/wildfly-10.0.0.Final
# The username who should own the process.
JBOSS_USER=wildfly
# The mode WildFly should start, standalone or domain
JBOSS_MODE=standalone
# Configuration for standalone mode
JBOSS_CONFIG=standalone.xml

Next, we will use the  chkconfig command to install WildFly as a service: the first command will add the wildfly shell script to the chkconfig list:

$ chkconfig --add wildfly

The second one, sets the boot levels where the service will be started:

$ chkconfig --level 2345 wildfly on

In order to test that the service starts correctly issue from any root/sudoer user:

$ sudo service wildfly start

And here’s the corresponding service stopping:

$ sudo service wildfly stop

WildFly 10 as a Service for Windows users

Installing WildFly as a service on Windows is much simpler than the older AS7/EAP6 counterpart. As a matter of fact it’s not necessary to install any third party native library because WildFly already ships with all you need. So move to the JBOSS_HOME/docs/contrib/scripts/service folder. If you want to install WildFly as a service in standalone mode simple issue from the command prompt:

service install

Now you can use the Windows Services tab in order to manage the service start/stop. As an alternative you can use the service command to perform basic service management (start/stop/restart). Example:

service restart

Installing WildFly in domain mode requires that you specify some additional settings such as the Domain controller (default 127.0.0.1 on port 9990) and the host name we are going to start (default “master”).

service install /controller localhost:9990 /host master

 

JBoss AS 7 as a Service for Windows Users

The latest release of the application server requires the following steps to be installed as a service:
Download the JBoss Web native connectors which contain the jbosssvc.exe and service.bat. These files are necessary to start JBoss AS 7 as service for Windows users.
You can download the latest stable release for your platform at:
http://www.jboss.org/jbossweb/downloads.html
install run jboss as service
Then unzip the file into your JBoss AS distribution directory.

Adapting service.bat

Now the little hack: as a matter of fact the service.bat file is based on the JBoss AS 5 platform. The most notable difference is that JBoss AS 7 uses standalone.bat instead of run.bat (although a run.bat file has been added for comtability reason).
So replace the service.bat with the following (just adapt your JBOSS_HOME variable to your environment):

@echo off
REM JBoss, the OpenSource webOS
REM
REM Distributable under LGPL license.
REM See terms of license at gnu.org.
REM
REM -------------------------------------------------------------------------
REM JBoss Service Script for Windows
REM -------------------------------------------------------------------------

@if not "%ECHO%" == "" echo %ECHO%
@if "%OS%" == "Windows_NT" setlocal
set DIRNAME=%CD%

REM
REM VERSION, VERSION_MAJOR and VERSION_MINOR are populated
REM during the build with ant filter.
REM
set SVCNAME=JBAS711SVC
set SVCDISP=JBoss Application Server 7.1.1
set SVCDESC=JBoss Application Server 7.1.1 GA/Platform: Windows x86
set NOPAUSE=Y

REM Suppress killing service on logoff event
set JAVA_OPTS=-Xrs
SET JBOSS_HOME=C:\jboss-as-7.1.1.Final
REM Figure out the running mode

if /I "%1" == "install"   goto cmdInstall
if /I "%1" == "uninstall" goto cmdUninstall
if /I "%1" == "start"     goto cmdStart
if /I "%1" == "stop"      goto cmdStop
if /I "%1" == "restart"   goto cmdRestart
if /I "%1" == "signal"    goto cmdSignal
echo Usage: service install^|uninstall^|start^|stop^|restart^|signal
goto cmdEnd

REM jbosssvc retun values
REM ERR_RET_USAGE           1
REM ERR_RET_VERSION         2
REM ERR_RET_INSTALL         3
REM ERR_RET_REMOVE          4
REM ERR_RET_PARAMS          5
REM ERR_RET_MODE            6

:errExplain
if errorlevel 1 echo Invalid command line parameters
if errorlevel 2 echo Failed installing %SVCDISP%
if errorlevel 4 echo Failed removing %SVCDISP%
if errorlevel 6 echo Unknown service mode for %SVCDISP%
goto cmdEnd

:cmdInstall
jbosssvc.exe -imwdc %SVCNAME% "%DIRNAME%" "%SVCDISP%" "%SVCDESC%" service.bat
if not errorlevel 0 goto errExplain
echo Service %SVCDISP% installed
goto cmdEnd

:cmdUninstall
jbosssvc.exe -u %SVCNAME%
if not errorlevel 0 goto errExplain
echo Service %SVCDISP% removed
goto cmdEnd

:cmdStart

REM Executed on service start

del .r.lock 2>&1 | findstr /C:"being used" > nul

if not errorlevel 1 (

  echo Could not continue. Locking file already in use.
  goto cmdEnd

)

echo Y > .r.lock

jbosssvc.exe -p 1 "Starting %SVCDISP%" > %JBOSS_HOME%\standalone\log\standalone.log
call standalone.bat --server-config=standalone.xml < .r.lock >> %JBOSS_HOME%\standalone\log\standalone.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> %JBOSS_HOME%\standalone\log\standalone.log
del .r.lock

goto cmdEnd

:cmdStop

REM Executed on service stop

echo Y > .s.lock

jbosssvc.exe -p 1 "Shutting down %SVCDISP%" > %JBOSS_HOME%\standalone\log\shutdown.log
call jboss-cli.bat --connect command=:shutdown >> %JBOSS_HOME%\standalone\log\shutdown.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> %JBOSS_HOME%\standalone\log\shutdown.log
del .s.lock

goto cmdEnd

:cmdRestart

REM Executed manually from command line
REM Note: We can only stop and start

echo Y > .s.lock

jbosssvc.exe -p 1 "Shutting down %SVCDISP%" >> %JBOSS_HOME%\standalone\log\shutdown.log
call jboss-cli.bat --connect command=:shutdown >> %JBOSS_HOME%\standalone\log\shutdown.log 2>&1
del .s.lock

:waitRun

REM Delete lock file

del .r.lock > nul 2>&1

REM Wait one second if lock file exist

jbosssvc.exe -s 1

if exist ".r.lock" goto waitRun

echo Y > .r.lock

jbosssvc.exe -p 1 "Restarting %SVCDISP%" >> %JBOSS_HOME%\standalone\log\standalone.log
call standalone.bat --server-config=standalone.xml < .r.lock >> %JBOSS_HOME%\standalone\log\standalone.log 2>&1
jbosssvc.exe -p 1 "Shutdown %SVCDISP% service" >> %JBOSS_HOME%\standalone\log\standalone.log

del .r.lock

goto cmdEnd

:cmdSignal

REM Send signal to the service.
REM Requires jbosssch.dll to be loaded in JVM

@if not ""%2"" == """" goto execSignal

echo Missing signal parameter.
echo Usage: service signal [0...9]

goto cmdEnd

:execSignal

jbosssvc.exe -k%2 %SVCNAME%

goto cmdEnd

:cmdEnd
Now from the DOS prompt run:
C:\jboss-as-7.1.1.Final\bin>service.bat install
Installed JBAS711SVC
Service JBoss Application Server 7.1.1 installed
This will install JBoss AS 7 in the list of Services.
run jboss as service

JBoss AS 7 as a Service for Linux users

Linux applications use System V Init scripts to run an application when the machine boots.
Basically, the scripts which start a service are placed in:
/etc/rc.d/init.d/   (other distributions: /etc/init.d/)

The same scripts need to be linked as:
/etc/rc.(x)/  (Prefixed with S for start and K for stop)

We suggest you reading a good tutorial about Linux Init scripts if you are new to this:
http://www.linux.com/news/enterprise/systems-management/8116-an-introduction-to-services-runlevels-and-rcd-scripts

In order to install JBoss AS 7 on your Linux distribution pickup the file jboss-as-standalone.sh located in JBOSS_HOME/bin/init.d. This file needs to be placed in init.d to launch jboss as a service.

cp jboss_as-standalone.sh /etc/rc.d/init.d/jboss					

Remember to edit the file jboss-as-standalone.sh to adapt it to your environment:

....
# Load JBoss AS init.d configuration.
if [ -z "$JBOSS_CONF" ]; then
  JBOSS_CONF="/etc/jboss-as/jboss-as.conf"
fi

[ -r "$JBOSS_CONF" ] && . "${JBOSS_CONF}"

# Set defaults.

if [ -z "$JBOSS_HOME" ]; then
  JBOSS_HOME=/usr/share/jboss-as
fi
export JBOSS_HOME
....
Now you should create a link for each System level so that Linux/Unix knows at which level the application server needs to be started:
ln -s /etc/rc.d/init.d/jboss /etc/rc3.d/S84jboss
ln -s /etc/rc.d/init.d/jboss /etc/rc5.d/S84jboss
ln -s /etc/rc.d/init.d/jboss /etc/rc4.d/S84jboss
ln -s /etc/rc.d/init.d/jboss /etc/rc6.d/K15jboss
ln -s /etc/rc.d/init.d/jboss /etc/rc0.d/K15jboss
ln -s /etc/rc.d/init.d/jboss /etc/rc1.d/K15jboss
ln -s /etc/rc.d/init.d/jboss /etc/rc2.d/K15jboss					
We don’t go deep into the details of Linux startup system but the links targeted as “S” are launched to start the service at a certain system level. The scripts marked with “K” are launched when the server is shuitting down.JBoss AS distribution includes a few shell commands which are located in the JBOSS_HOME/bin folder.

JBoss AS 4-5-6 as a Service for Windows Users

Earlier releases of JBoss ship out of the box with the service.bat shell command which is located into the JBOSS_HOME/bin folder. The service.bat commands needs at least one parameter to run:
C:\jboss-5.0.0.GA\bin>service
Usage: service install|uninstall|start|stop|restart|signal

In order to install JBoss AS as Windows service launch:

C:\jboss-5.0.0.GA\bin>service install
Now JBoss AS should me enlisted in the Windows Service List, which can be found in the control panel.

run jboss as service

Now you can use both the Windows service applet or the service.bat command to start the service. Example:

Starting JBoss AS service:

C:\jboss-5.0.0.GA\bin>service start

JBOSS AS 4-5-6 as a Service for Linux Users

The main difference with the AS 7 distribution is that you have a set of different shell command for each operating system:
jboss_init_hpux.sh
jboss_init_redhat.sh
jboss_init_suse.sh

Copy the appropriate file for your distribution into the /etc/rc.d/init.d folder

cp jboss_init_redhat.sh /etc/rc.d/init.d/jboss	

The rest of the procedure stays the same as for AS 7.

Reference: WildFly 10 Administration guide

Found the article helpful? if so please follow us on Socials