B4J Tutorial [ABMaterial] Raspberry Pi Service Setup

Discussion in 'B4J Tutorials' started by rwblinn, Nov 30, 2017.

  1. rwblinn

    rwblinn Well-Known Member Licensed User

    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):
    Code:
    cd /home/pi/weatherdashboard
    sudo java -jar /home/pi/weatherdashboard.jar
    with output example:
    Code:
    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.
    Code:
    cd /home/pi/weatherdashboard
    sudo nano weatherdashboard.sh
    with content:
    Code:
    #!/bin/bash
    echo Starting WeatherDashboard
    cd /home/pi/weatherdashboard
    ## Start WeatherDashboard
    /usr/bin/java -jar weatherdashboard.jar
    Make Script weatherdashboard.sh executable
    Code:
    cd /home/pi/weatherdashboard
    sudo chmod +x weatherdashboard.jar
    sudo chmod +x weatherdashboard.sh
    Test Script weatherdashboard.sh
    Run weatherdashboard.sh:
    Code:
    cd /home/pi/weatherdashboard
    ./weatherdashboard.sh
    Open the application in a webbrowser with url http://ip-address:51050/WeatherDashboard
    with output example:
    Code:
    Starting WeatherDashboard
    loading /home/pi/weatherdashboard:
    ...
    Create Service weatherdashboard.service
    Code:
    sudo nano /lib/systemd/system/weatherdashboard.service
    with content:
    Code:
    [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:
    Code:
    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:
    Code:
    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
    Code:
    sudo systemctl status weatherdashboard.service
    Start service
    Code:
    sudo systemctl start weatherdashboard.service
    Stop service
    Code:
    sudo systemctl stop weatherdashboard.service
    Check service log
    Code:
    sudo journalctl -f -u weatherdashboard.service
    Disable Service
    Code:
    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:
    Code:
    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
    Code:
    sudo systemctl status weatherdashboard.service
    Open for any further hints or items missed...
     
    Last edited: Dec 4, 2017
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice