B4J Question vps run as service helloworld.jar file

Discussion in 'B4J Questions' started by ajk, Jun 18, 2019.

  1. ajk

    ajk Active Member Licensed User

    Using
    Code:
    #!/bin/sh
    SERVICE_NAME=serverjava
    PATH_TO_JAR=/home/dd/hw.jar
    PID_PATH_NAME=/tmp/serverjava-pid

    case $1 in
    start)
           echo 
    "Starting $SERVICE_NAME ..."
      
    if [ ! -f $PID_PATH_NAME ]; then
           nohup /home/dd/jre/bin/java -jar $PATH_TO_JAR /tmp 
    2>> /dev/null >>/dev/null &     
                       echo $! > $PID_PATH_NAME 
           echo 
    "$SERVICE_NAME started ..."         
      
    else
           echo 
    "$SERVICE_NAME is already running ..."
      fi
    in serverjava.sh
    started by

    Code:
    [Unit]
     Description = Java 
    Service
     After network.target = serverjava.service

    [
    Service]
     
    Type = forking
     Restart=always
     RestartSec=
    1
     User=dd
     SuccessExitStatus=
    143
     ExecStart = /usr/local/bin/serverjava.sh start
     ExecStop = /usr/local/bin/serverjava.sh stop
     ExecReload = /usr/local/bin/serverjava.sh reload

    [Install]
     WantedBy=multi-user.target
    in serverjava.service
    produces


    The same hw.jar started manually
    by executing h.sh containing:
    Code:
    #!/bin/sh

    /home/dd/jre/bin/java -jar /home/dd/hw.jar
    gives positive response

    Everything happens on centos-7-x86_64.

    How to avoid this access deny that causes jetty HTTP error when system service starts? I have previously successfully run this with success on Ubuntu but have to change system due to Ubuntu stability issues.

    I would be very thankful for any help.
     
  2. aaronk

    aaronk Well-Known Member Licensed User

    Here is how I got it to work on my VPS running Linux and its running as a service. If for any reason the VPS reboots, then my B4J app will start automatically.

    I used SFTP and copied my B4J app to /opt/CloudServer/CloudServer.jar

    I then created a start-up script. This script will run as soon as the server boots up.

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

    Code:
    [Unit]
    Description=CloudServer 
    Service
    After=network.target

    [
    Service]
    Type=simple
    ExecStart=/usr/bin/java -jar /opt/CloudServer/CloudServer.jar
    StandardOutput=syslog
    StandardError=syslog

    SuccessExitStatus=
    143

    Restart=on-abort

    [Install]
    WantedBy=multi-user.target
    sudo systemctl enable CloudServer.service
    This will enable the service.

    sudo systemctl start CloudServer.service
    This will start the service.

    systemctl status CloudServer.service
    This will check the service to see if it's running.

    Hope this helps.
     
    f0raster0, OliverA, ajk and 2 others like this.
  3. ajk

    ajk Active Member Licensed User

    Thank you for reply: Your method works but it's disadvantage is that it do not monitor state of process.
    Service state is monitored by system & restarted if nedeed.
    After a few hours of fight I have forced my method to work - it was really access right issue
     
  4. OliverA

    OliverA Expert Licensed User

    Explain
    Might as well go into more detail, otherwise this is one more post were someone says "I fixed it!" without posting a proper fix. If someone else has this issue, they may not 100% know what to do, they only know that it can be fixed.
     
    f0raster0 likes this.
  5. ajk

    ajk Active Member Licensed User

    1) Have no explanation and I have to correct my post #3 - missed
    2) This was access right issue. Access rights for *.sh & www & *.jar were set wrong. After setting proper access rights - started. Interesting is that in form cited in #1 post worked on Ubuntu (9) it seems that Centos (7) have different "taste" about access right
     
    OliverA likes this.
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