Autostarting openHAB on Raspberry Pi

In “Installing openHAB Home Automation on Raspberry Pi” I have set up openHAB on a Raspberry Pi 2. But when I reboot it, I need to start openHAB manually. This post is about how to start openHAB automatically after a reboot.

openHAB running

openHAB running

Download the following zip file: https://github.com/mepi0011/openhab.doc/raw/master/examples/autostart.zip. It has two file: openhab and openhab.conf which are shown below:

The script file openhab:

#! /bin/sh
### BEGIN INIT INFO
# Provides: starts openhab from home
# Required-Start: $local_fs $network $named $portmap $remote_fs $syslog $time
# Required-Stop: $local_fs $network $named $portmap $remote_fs $syslog $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Kurze Beschreibung
# Description: Längere Bechreibung
### END INIT INFO
# Author:

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

if test -f /etc/default/openhab.conf; then
 . /etc/default/openhab.conf
else
 echo "Please set OPENHABPATH in /etc/default/openhab.conf"
 exit 1
fi

# Aktionen
case "$1" in
 start)
 if [ -f /var/run/openhab.pid ]; then
 echo "openhab seems to run allready. If not, please delete /var/run/openhab.pid"
 else

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

 echo Launching the openHAB runtime..
 java -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 -Djava.awt.headless=true -jar $cp -console &> /dev/null

 echo $! > /var/run/openhab.pid
 fi
 ;;
 stop)
 echo "stopping openhab"
 kill `cat /var/run/openhab.pid`
 rm /var/run/openhab.pid
 ;;
 restart)
 echo "does not work"
 ;;
esac

exit 0

In case if I’m using GPIO ports with openHAB, I need to unexport them (see https://github.com/openhab/openhab/wiki/GPIO-Binding)

So I add

echo 4 > /sys/class/gpio/unexport

to release GPIO pin 4 before killing openHAB in the stop section.

Place the file openhab into the /etc/init.d/ directory.

The configuration file openhab.conf:

# PATH TO OPENHAB
OPENHABPATH=/opt/openhab

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

💡 You might need to change the OPENHABPATH to match your openHAB installation folder.

Place this file openhab.conf into /etc/default/ directory.

Next, I need to set the proper definition. Change current directory to the init.d folder:

cd /etc/init.d

Change the file attributes so it can be executed:

sudo chmod a+x openhab

Change the group and owner:

sudo chgrp root openhab
sudo chown root openhab

Verify with

ls -al openhab

if the permissions look like this:

-rwxr-xr-x 1 root root 1821 Sep 30 2014 openhab

Change to the directory where we have copied openhab.cfg:

cd /etc/default

Insert the script into the run level with:

sudo update-rc.d openhab defaults

This now starts openHAB at boot time. openHAB can be stopped anytime with

sudo /etc/init.d/openhab start

Stopping openHAB works with

sudo /etc/init.d/openhab stop

To reboot, use

sudo reboot

If I ever want to undo this, openhab can be removed again from the autostart with

sudo update-rc.d -f openhab remove

I can check if openHAB is running checking if openhab.pid is present in /var/run with

ls /var/run
openHAB running

openHAB running

Summary

In order to start openHAB automatically, it requires a configuration and a script file. With the proper permissions set and inserted into the autostart sequence, openHAB gets started automatically at boot time, so I don’t need to start it manually.

Happy HABstarting 🙂

Links

15 thoughts on “Autostarting openHAB on Raspberry Pi

  1. Pingback: Blinky LED with openHAB on Raspberry Pi | MCU on Eclipse

  2. Receive error when I try to start with this:

    pi@raspberrypi:~ $ sudo /etc/init.d/openhab start
    : not found/openhab: 3: /etc/default/openhab.conf:
    /etc/init.d/openhab: 31: cd: can’t cd to /usr/share/openhab
    find: `./server’: No such file or directory
    Launching the openHAB runtime..
    pi@raspberrypi:~ $ Unrecognized option: -console
    Error: Could not create the Java Virtual Machine.
    Error: A fatal exception has occurred. Program will exit.

    Like

      • File exists and Java version appears to be OK

        pi@raspberrypi:~ $ ls -al /etc/default/openhab.conf
        -rw-r–r– 1 root root 118 Dec 28 03:23 /etc/default/openhab.conf

        pi@raspberrypi:~ $ cat /etc/default/openhab.conf
        # PATH TO OPENHAB
        OPENHABPATH=/usr/share/openhab

        # set ports for HTTP(S) server
        HTTP_PORT=8080
        HTTPS_PORT=8443
        pi@raspberrypi:~ $ ls -al /usr/share/openhab
        total 48
        drwxr-xr-x 8 openhab openhab 4096 Dec 25 22:15 .
        drwxr-xr-x 182 root root 4096 Dec 25 19:08 ..
        drwxr-xr-x 2 openhab openhab 4096 Dec 27 13:41 addons
        drwxr-xr-x 2 openhab openhab 4096 Dec 25 19:08 bin
        lrwxrwxrwx 1 openhab openhab 27 Jul 31 15:43 configurations -> /etc/openhab/configurations
        lrwxrwxrwx 1 openhab openhab 27 Jul 31 15:43 contexts -> /etc/openhab/jetty/contexts
        lrwxrwxrwx 1 openhab openhab 22 Jul 31 15:43 etc -> /etc/openhab/jetty/etc
        -rw-r–r– 1 openhab openhab 11232 Jul 31 15:43 LICENSE.TXT
        drwxr-xr-x 2 openhab openhab 4096 Dec 27 15:47 logs
        -rw-r–r– 1 openhab openhab 652 Jul 31 15:43 README.TXT
        drwxr-xr-x 6 openhab openhab 4096 Dec 28 03:33 server
        drwxr-xr-x 2 openhab openhab 4096 Dec 25 19:08 sounds
        drwxr-xr-x 4 openhab openhab 4096 Dec 25 19:08 webapps

        java version “1.8.0”
        Java(TM) SE Runtime Environment (build 1.8.0-b132)
        Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

        Like

  3. Pingback: Controlling NXP Freedom Board RGB LED with openHAB and Raspberry Pi | MCU on Eclipse

  4. openhab on raspberry pi 3,i configurate autostart,
    update-rc.d: error: openhab Default-Start contains no runlevels, aborting.
    how to config?

    Like

  5. Pingback: Hexiwear with Raspberry Pi and OpenHAB Home Automation | MCU on Eclipse

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.