Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Autostarting openHAB on Raspberry Pi

DZone's Guide to

Autostarting openHAB on Raspberry Pi

In my previous article, “Installing openHAB Home Automation on Raspberry Pi”, I 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.

· IoT Zone
Free Resource

In my previous article, Installing openHAB Home Automation on Raspberry Pi, I 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 files: 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

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 you 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:

Topics:
raspberry pi 2 ,raspberry pi ,iot ,home automation ,openhab

Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}