Skip to content

Commit

Permalink
[WFCORE-6935] Improve systemd service units
Browse files Browse the repository at this point in the history
Jira issue: https://issues.redhat.com/browse/WFCORE-6935

[WFCORE-6935] Minor fix for README

[WFCORE-6935] Correct naming for systemd, unit, file

[WFCORE-6935] Review launch.sh, add the ability to confirm the changes and allow specify areguments in any order

[WFCORE-6935] Apply changes after review
  • Loading branch information
yersan committed Nov 18, 2024
1 parent b7f831e commit ff100a9
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
= How to configure WildFly as a systemd service

The following steps describe the process to configure WildFly as a systemd service.
You can use the provided wildfly-[standalone,domain].service file as a template for your own systemd unit file.
It can be adjusted to your needs, for example, you can manually change the user that runs the service,
the location of the WildFly installation, logs, etc.
Alternatively, you can use the generate_systemd_unit.sh script to automatically generate a new systemd unit file
using this server installation as the WildFly home.

If you want to install WildFly as a systemd service from scratch as a systemd service, follow the steps below.

== Install WildFly and initialize the WILDFLY_HOME variable

# unzip -d /opt wildfly-34.0.0.Final.zip
# ln -s /opt/wildfly-34.0.0.Final /opt/wildfly
# WILDFLY_HOME=/opt/wildfly

== Standalone or Domain mode ?

# MODE=standalone

== Create a wildfly user and group

# groupadd -r wildfly
# useradd -r -g wildfly -d "${WILDFLY_HOME}" -s /sbin/nologin wildfly

== Configure systemd

# cd "${WILDFLY_HOME}/bin/systemd"
# cp "wildfly-${MODE}.conf" /etc/sysconfig/
# cp "wildfly-${MODE}.service" $(pkg-config systemd --variable=systemdsystemunitdir)
# chown -R wildfly:wildfly "${WILDFLY_HOME}"/
# chmod +x "${WILDFLY_HOME}/bin/systemd/launch.sh"
# systemctl daemon-reload

=== If SELinux is enabled, you need to set the appropriate context for the launch.sh script:

# sudo chcon -R -t bin_t "${WILDFLY_HOME}/"
# sudo semanage fcontext -a -t systemd_unit_file_t '${WILDFLY_HOME}(/.*)?'

== Start and enable WildFly

# systemctl start "wildfly-${MODE}.service"
# systemctl enable "wildfly-${MODE}.service"

== Check the status of WildFly

# systemctl status "wildfly-${MODE}.service"



= How to remove WildFly as a systemd service

== Standalone or Domain mode ?

# MODE=standalone

== Stop and disable WildFly

# systemctl stop "wildfly-${MODE}.service"
# systemctl disable "wildfly-${MODE}.service"

== If you are using SELinux, remove the custom context added

sudo semanage fcontext -d '${WILDFLY_HOME}(/.*)?'

== Remove WildFly systemd service

# rm -f "$(pkg-config systemd --variable=systemdsystemunitdir)/wildfly-${MODE}.service"
# rm -f "/etc/sysconfig/wildfly-${MODE}.conf"
# systemctl daemon-reload

== Remove WildFly installation

# rm -rf $(readlink "${WILDFLY_HOME}")
# rm -rf "${WILDFLY_HOME}"

== Remove WildFly user and group

# userdel wildfly
# groupdel wildfly
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/sh

# This script is just a helper to generate the systemd unit files for WildFly
# assuming that the server home will be the one that hold this script file.
# It also allows to specify the user and group that will run the server.

function print_usage() {
echo "INFO: Usage: "
echo "${SCRIPT_NAME} -m,--mode standalone|domain [-u,--user user] [-g,--group group] [-c,--confirm y|n]"

exit 1
}

SCRIPT_NAME=$(basename "${0}")
SCRIPT_DIR=$(dirname $(realpath "${0}"))
JBOSS_HOME=$(realpath "${SCRIPT_DIR}/../..")
JBOSS_SYSTEMD_MODE="${1}"

JBOSS_SYSTEMD_MODE=""
JBOSS_SYSTEMD_USER="wildfly"
JBOSS_SYSTEMD_GROUP="wildfly"
JBOSS_CONFIRM="n"

while [ "${#}" -gt 0 ]
do
# For each argument option, checks whether the argument length is zero or starts with hyphen(s)
case "${1}" in
-m|--mode)
if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then
echo "Error: Missing argument for ${1}" >&2
print_usage
fi
JBOSS_SYSTEMD_MODE="${2}"
shift 2
;;
-u|--user)
if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then
echo "Error: Missing argument for ${1}" >&2
print_usage
fi
JBOSS_SYSTEMD_USER="${2}"
shift 2
;;
-g|--group)
if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then
echo "Error: Missing argument for ${1}" >&2
print_usage
fi
JBOSS_SYSTEMD_GROUP="${2}"
shift 2
;;
-c|--confirm)
if [ -z "${2}" ] || [ ! "${2}" = `echo "${2}" | sed 's/^-*//'` ]; then
echo "Error: Missing argument for ${1}" >&2
print_usage
fi
JBOSS_CONFIRM="${2}"
shift 2
;;
*)
echo "Error: Unknown argument ${1}" >&2
print_usage
;;
esac
done

if [ "${JBOSS_SYSTEMD_MODE}" != 'standalone' ] && [ "${JBOSS_SYSTEMD_MODE}" != 'domain' ]; then
print_usage
fi

SYSTEMD_FILE="${SCRIPT_DIR}/wildfly-${JBOSS_SYSTEMD_MODE}.service"

echo "INFO: systemd unit file to generate: ${SYSTEMD_FILE}"
echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}"
echo "INFO: User: ${JBOSS_SYSTEMD_USER}"
echo "INFO: Group: ${JBOSS_SYSTEMD_GROUP}"

if [ 'y' != "${JBOSS_CONFIRM}" ] && [ 'Y' != "${JBOSS_CONFIRM}" ];then
while true; do
read -p "Do you want to generate ${SYSTEMD_FILE}? (y/n): " choice
case "${choice}" in
y|Y ) break;;
n|N ) echo "Operation cancelled."; exit 1;;
* ) ;;
esac
done
fi

sed -e "s|@@@JBOSS_SYSTEMD_SERVER_HOME@@@|${JBOSS_HOME}|g" \
-e "s|@@@JBOSS_SYSTEMD_USER@@@|${JBOSS_SYSTEMD_USER}|g" \
-e "s|@@@JBOSS_SYSTEMD_GROUP@@@|${JBOSS_SYSTEMD_GROUP}|g" \
"${SYSTEMD_FILE}.template" > "${SYSTEMD_FILE}"

systemd-analyze verify --recursive-errors=no "${SYSTEMD_FILE}"

echo ""
echo "INFO: systemd unit file generated."
echo "INFO: The ${JBOSS_SYSTEMD_USER}:${JBOSS_SYSTEMD_GROUP} are the user:group configured to launch the server. You have to ensure this user and group exist and have the necessary permissions to read and launch the server."
echo "INFO: Use ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf to configure the server environment."
echo "INFO: After configuring your environment, to install the server as a systemd service do the following:"
echo ""
echo "sudo cp ${SYSTEMD_FILE} $(pkg-config systemd --variable=systemdsystemunitdir)"
echo "sudo cp ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf /etc/sysconfig"
echo "sudo systemctl daemon-reload"
echo "sudo systemctl start $(basename "${SYSTEMD_FILE}")"
echo ""
echo "INFO: In case of issues, you can check the service logs with:"
echo "sudo journalctl -u $(basename "${SYSTEMD_FILE}")"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh
if [ "${WILDFLY_SYSTEMD_DEBUG}" == "true" ]; then
set -x
fi

echo "INFO: Systemd Unit File server launch script"

# Disable color output for the standard output
export JAVA_OPTS="${JAVA_OPTS} -Dorg.jboss.logmanager.nocolor=true"
export PATH="${JAVA_HOME}/bin:${PATH}"

logDir=$(dirname "${WILDFLY_CONSOLE_LOG}")
if [ ! -d "${logDir}" ]; then
mkdir -p "${logDir}"
fi

wildflyOpts="${WILDFLY_OPTS}"
if [ -n "${WILDFLY_SUSPEND_TIMEOUT}" ]; then
wildflyOpts="-Dorg.wildfly.sigterm.suspend.timeout=${WILDFLY_SUSPEND_TIMEOUT} ${wildflyOpts}"
fi

if [ -z "${WILDFLY_HOST_CONFIG}" ]; then
"${WILDFLY_SH}" -c "${WILDFLY_SERVER_CONFIG}" -b "${WILDFLY_BIND}" ${wildflyOpts} > "${WILDFLY_CONSOLE_LOG}" 2>&1
else
"${WILDFLY_SH}" --host-config="${WILDFLY_HOST_CONFIG}" -c "${WILDFLY_SERVER_CONFIG}" -b "${WILDFLY_BIND}" ${wildflyOpts} > "${WILDFLY_CONSOLE_LOG}" 2>&1
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# The configuration you want to run

# Location of java in the JRE (the default)
#JAVA_HOME=/usr/lib/jvm/jre
# Use the following for location of java in the SDK
#JAVA_HOME=/usr/lib/jvm/java


# Configure the suspend timeout. By default, the suspend timeout is disabled, meaning that the server will not wait
# in the SUSPENDING state for any in-flight requests. This property configures the maximum number of seconds to wait for
# in-flight requests to complete. A value of -1 means the server will wait indefinitely.
#
# See https://docs.wildfly.org/32/Admin_Guide.html#graceful-shutdown-from-an-os-signal
#
# Note that increasing this value will extend the time the service unit takes to shut down the server.
# If WILDFLY_SUSPEND_TIMEOUT is configured, ensure that the TimeoutStopSec directive in the systemd service unit
# is set to a value that allows the server to wait until the suspend timeout expires and the server is stopped.
#WILDFLY_SUSPEND_TIMEOUT=300

# Location of the server standard out, e.g /opt/wildfly/standalone/log/service-console.log
#WILDFLY_CONSOLE_LOG="/opt/wildfly/standalone/log/service-console.log"

# Define the script to use to start wildfly
#WILDFLY_SH="/opt/wildfly/bin/domain.sh"

# Define server configuration to start, eg. domain.xml
#WILDFLY_SERVER_CONFIG=domain.xml

# Define the host controller configuration, eg. host.xml
#WILDFLY_HOST_CONFIG=host.xml

# The address to bind to
#WILDFLY_BIND=0.0.0.0

# Additional server args to include on startup
# For example, to add two properties to the server:
#WILDFLY_OPTS="-Dproperty1=value1 -Dproperty2=value2"

# Enables debug traces for the server launch script used by the systemd service unit
#WILDFLY_SYSTEMD_DEBUG=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[Unit]
Description=WildFly (domain mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=/opt/wildfly/bin/domain.sh"
Environment="WILDFLY_SERVER_CONFIG=domain.xml"
Environment="WILDFLY_CONSOLE_LOG=/opt/wildfly/domain/log/systemd-console.log"
Environment="WILDFLY_HOST_CONFIG="
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-domain.conf

User=wildfly
Group=wildfly
LimitNOFILE=102642
ExecStart=/bin/sh -c "/opt/wildfly/bin/systemd/launch.sh"
TimeoutSec=600

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[Unit]
Description=WildFly (domain mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/domain.sh"
Environment="WILDFLY_SERVER_CONFIG=domain.xml"
Environment="WILDFLY_CONSOLE_LOG=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/domain/log/systemd-console.log"
Environment="WILDFLY_HOST_CONFIG="
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-domain.conf

User=@@@JBOSS_SYSTEMD_USER@@@
Group=@@@JBOSS_SYSTEMD_GROUP@@@
LimitNOFILE=102642
ExecStart=/bin/sh -c "@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/systemd/launch.sh"
TimeoutSec=600

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# The configuration you want to run

# Location of java in the JRE (the default)
#JAVA_HOME=/usr/lib/jvm/jre
# Use the following for location of java in the SDK
#JAVA_HOME=/usr/lib/jvm/java


# Configure the suspend timeout. By default, the suspend timeout is disabled, meaning that the server will not wait
# in the SUSPENDING state for any in-flight requests. This property configures the maximum number of seconds to wait for
# in-flight requests to complete. A value of -1 means the server will wait indefinitely.
#
# See https://docs.wildfly.org/32/Admin_Guide.html#graceful-shutdown-from-an-os-signal
#
# Note that increasing this value will extend the time the service unit takes to shut down the server.
# If WILDFLY_SUSPEND_TIMEOUT is configured, ensure that the TimeoutStopSec directive in the systemd service unit
# is set to a value that allows the server to wait until the suspend timeout expires and the server is stopped.
#WILDFLY_SUSPEND_TIMEOUT=300

# Location of the server standard out, e.g /opt/wildfly/standalone/log/service-console.log
#WILDFLY_CONSOLE_LOG="/opt/wildfly/standalone/log/service-console.log"

# Define the script to use to start wildfly
#WILDFLY_SH="/opt/wildfly/bin/standalone.sh"

# Define server configuration to start, eg. standalone.xml
#WILDFLY_SERVER_CONFIG=standalone.xml

# The address to bind to
#WILDFLY_BIND=0.0.0.0

# Additional server args to include on startup
# For example, to add two properties to the server:
#WILDFLY_OPTS="-Dproperty1=value1 -Dproperty2=value2"

# Enables debug traces for the server launch script used by the systemd service unit
#WILDFLY_SYSTEMD_DEBUG=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=WildFly (standalone mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=/opt/wildfly/bin/standalone.sh"
Environment="WILDFLY_SERVER_CONFIG=standalone.xml"
Environment="WILDFLY_CONSOLE_LOG=/opt/wildfly/standalone/log/systemd-console.log"
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-standalone.conf

User=wildfly
Group=wildfly
LimitNOFILE=102642
ExecStart=/bin/sh -c "/opt/wildfly/bin/systemd/launch.sh"
TimeoutSec=600

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=WildFly (standalone mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/standalone.sh"
Environment="WILDFLY_SERVER_CONFIG=standalone.xml"
Environment="WILDFLY_CONSOLE_LOG=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/standalone/log/systemd-console.log"
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-standalone.conf

User=@@@JBOSS_SYSTEMD_USER@@@
Group=@@@JBOSS_SYSTEMD_GROUP@@@
LimitNOFILE=102642
ExecStart=/bin/sh -c "@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/systemd/launch.sh"
TimeoutSec=600

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit ff100a9

Please sign in to comment.