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.

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

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