B4J Question vps run as service helloworld.jar file

ajk

Active Member
Licensed User
Longtime User
Using
B4X:
#!/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

B4X:
[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

HTTP ERROR 404
Problem accessing /FormExample.html. Reason:

Not Found
Powered by Jetty:// 9.4.z-SNAPSHOT


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

/home/dd/jre/bin/java -jar /home/dd/hw.jar

gives positive response

This is a static file, loaded from the www folder.

Examples:

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.
 

aaronk

Well-Known Member
Licensed User
Longtime 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

B4X:
[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.
 
Upvote 0

ajk

Active Member
Licensed User
Longtime 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
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Thank you for reply: Your method works but it's disadvantage is that it do not monitor state of process.
Explain
After a few hours of fight I have forced my method to work - it was really access right issue
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.
 
Upvote 0

ajk

Active Member
Licensed User
Longtime User
1) Have no explanation and I have to correct my post #3 - missed
Restart=on-abort

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
 
Upvote 0
Top