This page looks best with JavaScript enabled

Creating Red Hat Wildfly systemd service for Ubuntu 16.04

 ·  ☕ 3 min read  ·  ✍️ FJTC

Introduction

Starting from 15.04, Ubuntu replaced Upstart by systemd as stated in the Vivid Vervet release notes. Although Ubuntu is still able to run Upstart scripts, it is better to create new startup scripts targeting systemd in order to keep them working in the latest versions of Ubuntu, such as 16.04.

In this post I’ll describe how to start Red Hat Wildfly on startup on Ubuntu 16.04 by defining a systemd service.

Prerequisites

This post will not cover the installation of Wildfly itself, so I’ll assume that it is already installed in the directory and prepared to be executed by the user .

This means that the commands:

  • <wildfly home>/bin/standalone.sh
  • <wildfly home>/bin/jboss-cli.sh -connect command=shutdown

are ready to be executed by the user <wildfly user>.

Creating the service

On Ubuntu 16.04, the system services are located in the directory /etc/systemd/system. So, in order to create the service, it is necessary to create the file /etc/systemd/system/wildfly.service with the following contents:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Unit]
Description=Red Hat Wildfly Application Server
After=network-online.target

[Service]
User=<wildfly user>
ExecStart=<wildfly home>/bin/standalone.sh
ExecStop=<wildfly home>/bin/jboss-cli.sh --connect command=shutdown

[Install]
WantedBy=multi-user.target

It is very important to replace the values of <wildfly user> and <wildfly home> to the actual values. Furthermore, the value of <wildfly home> must be absolute (start with /).

Once the file is created, execute the command:

$ systemctl daemon-reload

This should load the newly created service. In order to check it the service is correctly defined, run the command:

$ systemctl status wildfly.service

If everything is right, the output of this command will be something like:

wildfly.service - Red Hat Wildfly Application Server
   Loaded: loaded (/etc/systemd/system/wildfly.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Now the service is installed and can be tested.

Testing the service

In order to test the service, run the command:

$ systemctl start wildfly.service

This command will return immediately. In order to see if it worked, execute the command:

$ systemctl status wildfly.service

this should output something like:

wildfly.service - Red Hat Wildfly Application Server
   Loaded: loaded (/etc/systemd/system/wildfly.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2016-08-25 19:48:35 BRT; 3s ago
 Main PID: 6451 (standalone.sh)
    Tasks: 108
   Memory: 284.5M
      CPU: 9.097s
   CGroup: /system.slice/wildfly.service
           ├─6451 /bin/sh /home/fjtc/jboss/wildfly-10.0.0.Final/bin/standalone.sh
           └─6496 java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.bytemafdafs

Stop the service by executing the command:

$ systemctl stop wildfly.service

The command:

$ systemctl status wildfly.service

can also be used to see if the stop command really worked.

Starting service on startup

By default, the wildfly.service will not be start automatically on startup because it is marked as disabled as stated by the status command:

...
Loaded: loaded (/etc/systemd/system/wildfly.service; disabled; vendor preset: enabled)
...

In order to mark it to start at boot, set it to enabled by running the command:

$ systemctl enable wildfly.service

This should change the status to enabled:

...
Loaded: loaded (/etc/systemd/system/wildfly.service; enabled; vendor preset: enabled)
...

If you want to disable the start at boot, just disable it by running the command:

$ systemctl disable wildfly.service

This should change the status to disabled and thus it will no longer be loaded at boot.

References

  • systemd – Official Homepage
  • man pages:
    • systemd.service(5)
    • systemd.unit(5)
Share on

FJTC
WRITTEN BY
FJTC
Co-Founder of InterlockLedger Network