Pas Home - Index

 

Hi, this is my lite script to start OpenHAB daemon at startup

 

Very usefull command during dev

sudo /etc/init.d/openhab.sh start

sudo /etc/init.d/openhab.sh stop

sudo /etc/init.d/openhab.sh restart

 

 

How to

 

#Transfert the openhab.sh script into /opt/openhab/openhab.sh or directly in /etc/init.d/openhab.sh

sudo cp /opt/openhab/openhab.sh /etc/init.d

 

# Make sure the script is executable (chmod again).

sudo chmod 777 /etc/init.d/openhab.sh

 

#At this point you should be able to start/stop/restart using the command

#   sudo /etc/init.d/openhab.sh start

#check its status with the status argument

#   /etc/init.d/openhab.sh status

#and stop it with

#   sudo /etc/init.d/openhab.sh stop

#and restart it with

#   sudo /etc/init.d/openhab.sh restart

 

# To make the Raspberry Pi use your init script at the right time, one more step is required: running the command

sudo update-rc.d openhab.sh defaults

#sudo update-rc.d -f openhab.sh remove

 

# This command adds in symbolic links to the /etc/rc.x directories so that the init script is run at the default times. you can see these links if you do

# ls -l /etc/rc?.d/*openhab.sh

 

openhab.sh script

#!/bin/sh
### BEGIN INIT INFO
# Provides:          openhab
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OpenHAB Daemon
# Description: Build your smart home in no time!
### END INIT INFO


ROOT_DIR=/opt/openhab


# set path to eclipse folder. If local folder, use '.'; otherwise, use /path/to/eclipse/
eclipsehome="$ROOT_DIR/server";


# set ports for HTTP(S) server
HTTP_PORT=8080
HTTPS_PORT=8443


# get path to equinox jar inside $eclipsehome folder
cp=$(find $eclipsehome -name "org.eclipse.equinox.launcher_*.jar" | sort | tail -1);




# 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.
#USER=root
DAEMON_USER=root
DAEMON_NAME=openhab


DAEMON=/usr/bin/java
DAEMON_ARGS="-Djna.boot.library.path=/usr/lib/jni -Dgnu.io.rxtx.SerialPorts=/dev/ttyAMA0 -Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=$HTTP_PORT -Djetty.port.ssl=$HTTPS_PORT -Djetty.home=. -Dlogback.configurationFile=configurations/logback.xml -Dfelix.fileinstall.dir=addons -Djava.library.path=lib -Djava.security.auth.login.config=./etc/login.conf -Dorg.quartz.properties=./etc/quartz.properties -Dequinox.ds.block_timeout=240000 -Dequinox.scr.waitTimeOnBlock=60000 -Dfelix.fileinstall.active.level=4 -Djava.awt.headless=true -jar $cp $* -console "
PIDFILE=/var/run/$DAEMON_NAME.pid


. /lib/lsb/init-functions

do_start() {      
  log_daemon_msg "Starting system $DAEMON_NAME daemon"
  start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --chdir $ROOT_DIR --exec $DAEMON -- $DAEMON_ARGS
  log_end_msg $?
}
do_stop() {
  log_daemon_msg "Stopping system $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