B4J Tutorial [GCE] For Noobs, Part 10 - Running Your B4J App As A Service and Automatically Starting At Boot

Note: The process outlined in this tutorial assumes that your Linux distribution is using systemd. If you have chosen Debian or Ubuntu as your Linux distribution when creating the VM then you should be good to go. Otherwise, you may try the process outlined here but I will not guarantee any success.

Update: 05 Nov 2017, Thanks to @OliverA for pointing out that an exit status should be added to the Service Script. The below service script has been updated to include the following line
SuccessExitStatus=143
This will have the effect of gracefully exiting the service instead of being reported as an error.
(source: https://stegard.net/2016/08/gracefully-killing-a-java-process-managed-by-systemd/)


Why would you want to run your B4J console app as a service?

Good question! Well the short answer is ‘It’s a better way’. The long answer is ‘It really is a better way’. But seriously, having your app running as a service provides some nice benefits. One of the nice benefits is that your app can be automatically started after a server reboot or after an application crash.

So how do we get our app to run as a service AND have it automatically start after a server reboot AND/OR restart again after the app crashes? With a carefully hand crafted service definition of course! :)

Creating a service definition is reasonably simple - for someone that lives and breathes Linux/Unix every day. But, for the rest of us noobs it takes a lot of searching and checking and crying out in frustration.

Follow the steps below to get started with your service. Note - you’ll need to do a lot of typing (just kidding - copy and paste works too) :)
  • Luckily for us the days of using cryptic text editors in Linux (eg. vi - a popular editor used by system administrators) are mostly behind us. We’ll be using a full-screen text editor called ‘nano’. nano is already installed on your VM - yay!

    The service definition is required to be created in the /etc/systemd/system directory. Type the following command to get started

    sudo nano /etc/systemd/system/myapp.service

    (change myapp to a more suitable name for your service)

  • Now copy and paste the following into the editor (to paste the text in the editor use ctrl-v)
    (replace the ‘myapp’ references below with your app’s name)
    (also replace /the/path/to/your/jar/ with the path to your jar - refer to the previous section Installing A B4J Console App - where you are reminded to make a note of where you installed the jar file)

    [Unit]
    Description=myapp Service
    After=network.target

    [Service]
    Type=simple
    ExecStart=/usr/bin/java -jar /the/path/to/your/jar/myapp.jar
    SuccessExitStatus=143

    Restart=on-abort

    [Install]
    WantedBy=multi-user.target


  • Save the file by pressing ctrl-x
    You will be prompted to save the file (yes or no) - type in ‘y’ and press return
    You may also be given the opportunity to save the file to a different name. If you are then just press Enter/Return to keep the current name.
That’s it for the service definition!

Start At Boot
To allow your newly created service to run at boot type (replace ‘myapp’ with the name you chose above)

sudo systemctl enable myapp.service​

An example of the above command

[email protected]:~/my-super-app$ sudo systemctl enable my-super-app.service
Created symlink /etc/systemd/system/multi-user.target.wants/my-super-app.service → /etc/systemd/system/my-super-app.service.​

Disable Start At Boot
To disable your service from starting at boot type (replace ‘myapp’ with the name you chose above)

sudo systemctl disable myapp.service​

An example of the above command

[email protected]:~/my-super-app$ sudo systemctl disable my-super-app.service
Removed /etc/systemd/system/multi-user.target.wants/my-super-app.service.​

Starting The Service
To start running the service type (replacing ‘myapp’ with the name you chose above)

sudo systemctl start myapp.service​

Stopping The Service
If you want to stop the service then type (replacing ‘myapp’ with the name you chose above)

sudo systemctl stop myapp.service​

Restarting The Service
If you want to restart the service then type (replacing ‘myapp’ with the name you chose above)

sudo systemctl restart myapp.service​

Checking The Status Of A Service
At times you may need to check the status of the service. The command below will let you do just that. To check the status of a service type the following at the command line (replacing ‘myapp’ with the name you chose above)

system status myapp.service​

An example of the output of the above command

[email protected]:~/my-super-app$ systemctl status my-super-app.service
● my-super-app.service - My Super App
Loaded: loaded (/etc/systemd/system/my-super-app.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2017-11-03 13:04:13 UTC; 21s ago
Main PID: 11598 (java)
Tasks: 11 (limit: 4915)
CGroup: /system.slice/my-super-app.service
└─11598 /usr/bin/java -jar /home/youruserid/my-super-app/GCEDemo.jar

Nov 03 13:04:25 web-server-1 java[11598]: At the 3rd stroke the time will be 13:04:25
Nov 03 13:04:26 web-server-1 java[11598]: At the 3rd stroke the time will be 13:04:26
...​

In the above example you can see that the status is showing as ‘...active (running)...’

Also shown is path of the program/process for the app that is running as the service. At the bottom of the output will be shown the last few lines of the output of your app - by ‘output’ I am referring to the Log() statements that you may have in your app.

One last thing worth noting is the status indicating if the service has been enabled to start at boot. You can see this in the above command output on the 3rd line (starting with ‘Loaded:...’) and in this example the service is ‘; disabled;’ at boot. If the service was enabled to start at boot it would display ‘; enabled;’.

Next, we will discuss rules, Firewall rules to be more specific. ;)
 
Last edited:
Top