Tuesday, September 29, 2015

Adding WildFly as systemd service

In Centos 7, SystemD has finally taken the center stage and become the defacto method to start/stop services. It is fully compatible with the legacy init scripts though, which gave me an interesting dilemma. I know that systemd is supposed to be superior in many ways but why bother learning it if the old method works just fine.

Anyway, recently, my team got the opportunity to do a complete middleware upgrade and migration to a brand new server running Centos 7. Wanting to take full advantage of the situation, we've decided to take the plunge and use SystemD to start/stop our Wildfly app server.

To my pleasant surprise, this had proven to be relatively painless and straightforward. All we needed to do was to create a service file in /usr/lib/systemd/system. You can give the file any name you want but in our case, we named ours wildfly.service.

Here's what our service (unit) file looks like:
[Unit]
Description=WildFly application server for Fusion App
After=network.target
After=postgresql-9.4.service

[Service]
Type=simple
User=jboss
Group=jboss
ExecStart=/opt/jboss/wildfly-9.0.1.Final/bin/startapp.sh

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

Notice how easy it is to define service dependencies. No more priority levels to deal with to determine startup sequence.

As defined in the ExecStart command, the service starts by invoking startapp.sh. There's no need to define ExecStop command as systemd will simply issue a kill signal to the Wildfly process. This will stop the app server.

Here's what our startapp.sh script looks like.
#!/bin/sh
#Script: startapp.sh

# Load Java configuration.
[ -r /etc/java/java.conf ] && . /etc/java/java.conf
export JAVA_HOME

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

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

$JBOSS_HOME/bin/standalone.sh -Djboss.server.base.dir=$JBOSS_HOME/myapp -Dorg.jboss.boot.log.file=$JBOSS_HOME/myapp/log/boot.log -Dlogging.configuration=$JBOSS_HOME/myapp/configuration/logging.properties

Finally, here's a list of related systemd commands.
#Start the service
systemctl start wildfly

#Stop the service
systemctl stop wildfly

#Enable the service on startup
systemctl enable wildfly

#Disable the service on startup
systemctl disable wildfly

#Check if service is enabled on startup
systemctl is-enabled wildfly

#Totally disable the service 
systemctl mask wildfly

Easy. No more fiddling with chkconfig :).