B4J Question Raspiberry Pi how to auto start java server app?

dragonguy

Active Member
Licensed User
Longtime User

Siam

Active Member
Licensed User
Longtime User
Hello,

put this script to /etc/init.d/<name of your start script>

#!/bin/sh
### BEGIN INIT INFO
# Provides: Für welches Programm ist das Script?
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Kurze Beschreibung
# Description: Längere Beschreibung
### END INIT INFO

# Actions
SERVICE_NAME=<PUT HERE THE NAME OF THE SERVICE> Sample ; My Service
PATH_TO_JAR=<PUT HERE THE PATH TO YOUR JAR> Sample: /var/www/html/gpio.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
# Change Here the filename of the .log file
# if no Logfile is neded then comment the next line with #

nohup java -jar $PATH_TO_JAR /tmp 2>> /var/log/gpio.log >> /var/log/gpio.log &
# and uncommend folowing line to start without logfile
# nohup 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
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
Step2:
sudo chmod +x /etc/init.d/<name of your start script>

Step3:
sudo update-rc.d <name of your start script> defaults

Step4:
Test Your script with:

sudo /etc/init.d/<name of your start script> start
sudo /etc/init.d/<name of your start script> stop
sudo /etc/init.d/<name of your start script> restart


regards

Andy
 
Last edited:
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
startscript i got message here

B4X:
pi@raspberrypi /etc/init.d $ sudo /etc/init.d/alarm_server.sh start
Starting AlarmServer ...
AlarmServer is already running ...
pi@raspberrypi /etc/init.d $ sudo /etc/init.d/alarm_server.sh stop
AlarmServer stoping ...
/etc/init.d/alarm_server.sh: 34: kill: No such process

AlarmServer stopped ...

i use top to check

B4X:
Tasks:  90 total,   1 running,  89 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.7 us,  2.0 sy,  0.0 ni, 97.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:    445032 total,   161532 used,   283500 free,    19344 buffers
KiB Swap:   102396 total,        0 used,   102396 free,    88756 cached

  PID USER      PR  NI  VIRT  RES  SHR S  %CPU %MEM    TIME+  COMMAND
2578 pi        20   0  4672 2396 2012 R   1.0  0.5   0:09.51 top
1605 root      20   0  1752 1224 1140 S   0.3  0.3   0:00.29 ifplugd
2148 root      20   0 21452  10m 7016 S   0.3  2.3   0:02.24 Xorg
2329 pi        20   0  113m  21m  19m S   0.3  5.0   0:08.53 lxpanel
    1 root      20   0  2148 1360 1256 S   0.0  0.3   0:01.86 init
    2 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kthreadd
    3 root      20   0     0    0    0 S   0.0  0.0   0:00.25 ksoftirqd/0
    5 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kworker/0:0H
    6 root      20   0     0    0    0 S   0.0  0.0   0:00.06 kworker/u2:0
    7 root      20   0     0    0    0 S   0.0  0.0   0:01.23 rcu_preempt
    8 root      20   0     0    0    0 S   0.0  0.0   0:00.00 rcu_sched
    9 root      20   0     0    0    0 S   0.0  0.0   0:00.00 rcu_bh
   10 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 khelper
   11 root      20   0     0    0    0 S   0.0  0.0   0:00.01 kdevtmpfs
   12 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 netns
   13 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 perf
   14 root      20   0     0    0    0 S   0.0  0.0   0:00.00 khungtaskd
   15 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 writeback
   16 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 crypto
   17 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 bioset
   18 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kblockd
   19 root      20   0     0    0    0 S   0.0  0.0   0:01.57 kworker/0:1
   20 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 rpciod
   21 root      20   0     0    0    0 S   0.0  0.0   0:00.00 kswapd0
   22 root      20   0     0    0    0 S   0.0  0.0   0:00.00 fsnotify_mark
   23 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 nfsiod
   29 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 kthrotld
   30 root       1 -19     0    0    0 S   0.0  0.0   0:00.00 VCHIQ-0
   31 root       1 -19     0    0    0 S   0.0  0.0   0:00.00 VCHIQr-0
   32 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 VCHIQs-0
   33 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 iscsi_eh
   34 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 dwc_otg
   35 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 DWC Notificatio
   37 root      20   0     0    0    0 S   0.0  0.0   0:00.00 VCHIQka-0
   38 root      10 -10     0    0    0 S   0.0  0.0   0:00.00 SMIO
   39 root       0 -20     0    0    0 S   0.0  0.0   0:00.00 deferwq
   40 root      20   0     0    0    0 S   0.0  0.0   0:00.29 kworker/u2:2

cant find my application
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
here is my script
B4X:
#!/bin/sh
### BEGIN INIT INFO
# Provides: Alarm Server
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Alarm Server
# Description: Alarm Server
### END INIT INFO

# Actions
SERVICE_NAME=AlarmServer
PATH_TO_JAR=/home/pi/Alarm_Server/alarmserver.jar
PID_PATH_NAME=/tmp/AlarmServer-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
# Change Here the filename of the .log file
# if no Logfile is neded then comment the next line with #
# nohup java -jar $PATH_TO_JAR /tmp 2>> /var/log/gpio.log >> /var/log/gpio.log &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
logfile here
B4X:
main._appstart (java line: 65)
java.io.FileNotFoundException: /config.txt (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:131)
        at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:225)
        at anywheresoftware.b4a.objects.streams.File.ReadMap2(File.java:322)
        at anywheresoftware.b4a.objects.streams.File.ReadMap(File.java:309)
        at b4j.example.main._appstart(main.java:65)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl$
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcce$
        at java.lang.reflect.Method.invoke(Method.java:483)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
        at b4j.example.main.main(main.java:29)

config.txt store at /home/pi/Alarm_server/
same location of jar file.
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
one more problem about raspberry pi now auto start my app in background when boot, how can i bring it back to foreground?
i want to see my dataon the screen.
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
@Erel where i got the nohup.out file?
i always get this message
B4X:
pi@raspberrypi ~/Alarm_Server $ sudo tail -f nohup.out
tail: cannot open `nohup.out' for reading: No such file or directory
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i always get this message
cause you are redirecting the standard out /dev/null instead of to nohup.out?
Sorry, if i get it wrong; my linux knowledge is a really basic one
 
Upvote 0
Top