### Start B4J WebApp as Daemon

This is an example on how to install and start a WebApp as a Daemon on a Raspberry Pi.
B4J Project rswitchit - Switch devices remote
Copyright (c) 2014 by Robert W.B. Linn, Pinneberg, Germany
These apps are made available under the terms of the Mozilla Public License 2.0 ( https://www.mozilla.org/MPL/2.0 ).
Purpose:
To start rswitchit.jar as daemon, when the Raspberry Pi boots up.
Installation:
• Login as Root
• Goto folder /var/run
• nano rswitchit.pid, enter 6849 and save the pid file (^O, ^X)
• Goto folder /etc/init.d
• Copy from your development device www/sh/rswitchitd to /etc/init.d or create new from the content below under script
• Test if the script is correct: bash -n rswitchitd
• If script error, correct using nano rswitchitd. If format (=conversion dos) error, see below under Test Script Conversion Error.
• Make rswitchitd executable: sudo chmod 755 /etc/init.d/rswitchitd
• Register the rswitchitd script to be run at startup: sudo update-rc.d rswitchitd defaults
• Reboot Raspberry Pi and check last entry after boot showing rswitchit is started
Summary/Additional Information for script switchd
Test Script Syntax:
Goto the /etc/init.d/ folder:
bash -n rswitchitd
Test Script Start with Echo On:
sudo sh -x /etc/init.d/rswitchitd start
Test Script Conversion Error:
If an error occurs, like unexpected token at line NN '$1" ..., then the file format is not converted from dos to unix.
Options to correct:
a) if usign UltraEdit, convert the file via File > Conversions > DOS to UNIX. Then save the file.
b) use dos2unix on the RasPi = handy tool to convert is dos2unix (for commands see http://linuxcommand.org/man_pages/dos2unix1.html ).
To install use: sudo apt-get install dos2unix
To convert from dos to unix format: dos2unix -n rswitchitd rswitchitd2. Then delete rswitchitd and rename rswitchitd2 to rswitchitd
Make Script executable:
sudo chmod 755 /etc/init.d/rswitchitd
Test Script start:
sudo /etc/init.d/rswitchitd start
Test Script stop:
sudo /etc/init.d/rswitchitd stop
Register Script to be run at start-up:
sudo update-rc.d rswitchitd defaults
Remove Script from Startup sequence:
Bit tricky as various options
1) sudo update-rc.d rswitchitd disable ;because it keeps the rc.d symlinks but with the K (killed, stopped) prefix. Revert it with an enable.
2) chmod -x the /etc/init.d/rswitchitd
3) insert an exit 0 at the top of rswitchitd
4) using tool sysv-rc-conf:
Install: apt-get install sysv-rc-conf
Running sysv-rc-conf: Lists the scripts executed at which runlevel. Enable/disable services via space and quit with q.
5) This command will not do the job: sudo update-rc.d -f rswitchitd remove
Check Process:
Check the processes and lookup for rswitchit.jar if running.
ps -ef
Example output:
root 2507 1 3 08:56 ? 00:00:10 /usr/bin/java -jar rswitchit.jar
To stop the process: kill 2507
Notes:
• The header at the start is to make the script LSB compliant and provides details about the start up script and you should only need to change the name.
If you want to know more about creating LSB scripts for managing services, see http://wiki.debian.org/LSBInitScripts
• If warning LSB Mathematica popsup, consider to remove Mathematica frm WolframAlpha: sudo apt-get remove wolfram-engine
• In the script it is important to change the folder to the rswitchit folder (eg DIR=...), else rswitchit will crash as can not create the log file.
• If the process needs to be stop, use ps -ef, lookup for the rswitchitd pid and then kill pid
*** THE SCRIPT *** See rswitchitd for latest version
!/bin/bash
/etc/init.d/rswitchitd
## BEGIN INIT INFO
Provides: rswitchit
Required-Start: $remote_fs $syslog
Required-Stop: $remote_fs $syslog
Default-Start: 2 3 4 5
Default-Stop: 0 1 6
Short-Description: Switch devices remote.
Description: rswitchit start / stop a program a boot / shutdown.
## END INIT INFO
Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/home/pi/b4j/rswitchit
JAVACMD=/usr/bin/java
WEBAPP=rswitchit.jar
LOG=$DIR/rswitchit.log
DAEMON=$DIR/rswitchit.jar
DAEMON_NAME=rswitchit
PIDFILE=/var/run/rswitchit.pid
This next line determines what user the script runs as.
Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=pi
The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
. /lib/lsb/init-functions
echo -n > $LOG
do_start () {
log_daemon_msg "Starting WebApp $DAEMON_NAME daemon"
start-stop-daemon --start --name $WEBAPP --background --pidfile $PIDFILE --make-pidfile --chdir $DIR --exec $JAVACMD -- -jar $WEBAPP >> $LOG --user $DAEMON_USER --chuid $DAEMON_USER
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping WebApp $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}
case "$1" in
start|stop)
do_${1}
;;
restart|reload|force-reload)
do_stop
do_start
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;
esac
exit 0

Changed: 2018-06-07 09:28, Category: WebApps, File: T6C277M2078.htm, ID: 2078