B4J Tutorial [ABMaterial] Raspberry Pi Service Setup

Objectives
To install a service on a Raspberry Pi enabling start, stop, status and autostart an ABMaterial application.
The tutorial uses an ABMaterial v4.00 example application [to explore how to use ABMaterial with custom components JustGage and Chart.js], called WeatherDashboard, but can be applied to any other application.

The setup has been tested on a Raspberry Pi 3 with Raspian Stretch (Linux 4.9.59-v7+ arm LE).

Prepare
Login as user Pi.
Create folder /home/pi/weatherdashboard
Copy the required application files, including subfolder www to /home/pi/weatherdashboard.
The folder /home/pi/weatherdashboard contains:
files: weatherdashboard.jar, weatherdashboard.set, copymewithjar.needs, robots.txt
folders: logs, www

Make changes to weatherdashboard.set accordingly.

Test
After copying the required files & folder, perform a first test if the application is running, from a terminal (i.e. opened with PuTTY):
B4X:
cd /home/pi/weatherdashboard
sudo java -jar /home/pi/weatherdashboard.jar

with output example:
B4X:
cd weatherdashboard
sudo java -jar weatherdashboard.jar
loading /home/pi/weatherdashboard: copymewithjar.needs...
Using cache system: 3.0
Needs material/awesome icons
Building core.min.1511971058261.css...
2017-11-29 16:57:39.683:INFO::main: Logging initialized @2700ms to org.eclipse.jetty.util.log.StdErrLog
2017-11-29 16:57:40.233:INFO:oejs.Server:main: jetty-9.4.z-SNAPSHOT
2017-11-29 16:57:40.558:INFO:oejs.session:main: DefaultSessionIdManager workerName=node0
...

The application is running and starts to sample data.
Open webbrowser and enter as url (change the ip address): http://ip-address:51050/WeatherDashboard
The Weather Dashboard should be displayed, showing already some sampled data.

Next is to define the application as a service with start, stop & status functionality.
Note: the application path & name is in lowercase.

Create Script weatherdashboard.sh
This script file is used to start the service. Important is to start the service from the weatherdashboard folder.
B4X:
cd /home/pi/weatherdashboard
sudo nano weatherdashboard.sh
with content:
B4X:
#!/bin/bash
echo Starting WeatherDashboard
cd /home/pi/weatherdashboard
## Start WeatherDashboard
/usr/bin/java -jar weatherdashboard.jar

Make Script weatherdashboard.sh executable
B4X:
cd /home/pi/weatherdashboard
sudo chmod +x weatherdashboard.jar
sudo chmod +x weatherdashboard.sh

Test Script weatherdashboard.sh
Run weatherdashboard.sh:
B4X:
cd /home/pi/weatherdashboard
./weatherdashboard.sh
Open the application in a webbrowser with url http://ip-address:51050/WeatherDashboard
with output example:
B4X:
Starting WeatherDashboard
loading /home/pi/weatherdashboard:
...

Create Service weatherdashboard.service
B4X:
sudo nano /lib/systemd/system/weatherdashboard.service
with content:
B4X:
[Unit]
Description=WeatherDashboard Service
After=network.target

[Service]
Type=simple
ExecStart=/home/pi/weatherdashboard/weatherdashboard.sh
SuccessExitStatus=143
Restart=on-abort

[Install]
WantedBy=multi-user.target

Activate weatherdashboard.service
A few more steps required, to activate the service:
B4X:
sudo chmod 644 /lib/systemd/system/weatherdashboard.service
sudo systemctl enable weatherdashboard.service
sudo systemctl start weatherdashboard.service
sudo systemctl status weatherdashboard.service
with output example:
B4X:
weatherdashboard.service - WeatherDashboard Service
  Loaded: loaded (/lib/systemd/system/weatherdashboard.service; enabled; vendor preset: enabled)
  Active: active (running) since Wed 2017-11-29 19:10:35 CET; 3s ago
Main PID: 8261 (weatherdashboar)
  CGroup: /system.slice/weatherdashboard.service
  ├─8261 /bin/bash /home/pi/weatherdashboard/weatherdashboard.sh
  └─8262 /usr/bin/java -jar weatherdashboard.jar

Nov 29 19:10:38 59 weatherdashboard.sh[8261]: 2017-11-29 19:10:38.126:INFO:oejsh.ContextHandler:main: Stopped o.e.j.
...

Summary Service Commands
Check status
B4X:
sudo systemctl status weatherdashboard.service
Start service
B4X:
sudo systemctl start weatherdashboard.service
Stop service
B4X:
sudo systemctl stop weatherdashboard.service
Check service log
B4X:
sudo journalctl -f -u weatherdashboard.service
Disable Service
B4X:
sudo systemctl disable weatherdashboard.service
Hints
Ownership
Check if all files & folders in /home/pi/weatherdashboard have pi as the owner and not root!
If not the case, then change ownership using chown. Examples:
B4X:
sudo chown pi logs
sudo chown pi weatherdashboard.sh
...

Reviews Regulary
Review or cleanup the weatherdasboard log files located in folder /home/pi/weatherdashboard/logs.
Check the status of the application by running
B4X:
sudo systemctl status weatherdashboard.service

Open for any further hints or items missed...
 
Last edited:
Top