Skip to content

Instantly share code, notes, and snippets.

@andreacioni
Last active November 24, 2017 18:08
Show Gist options
  • Save andreacioni/f1cf0b5e415fe2873a520a938a91cd76 to your computer and use it in GitHub Desktop.
Save andreacioni/f1cf0b5e415fe2873a520a938a91cd76 to your computer and use it in GitHub Desktop.
OpenHab 2 start on boot script
#! /bin/sh
### BEGIN INIT INFO
# Provides: openHAB
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop openHAB in screen Session
# Description: This runs openHAB continuously in screen.
### END INIT INFO
# Set OH-User
OHUSER=your_useraname #MUST be updated with your username
# Set OH-Path
OHPATH=/your/path/openhab-offline-2.0.0-SNAPSHOT #MUST be updated with your openahb 2 directory, where there is start.sh
# uncomment and set if necessary to obtain correct Codepage and language
# export LC_ALL=de_DE.UTF-8
#INSTALL
#sudo chmod a+x /etc/init.d/openhab
#sudo update-rc.d openhab defaults
#ACCESSING
#sudo -u <OHUSER the same defined above> screen -r openHAB
#UNINSTALL
#sudo update-rc.d -f openhab remove
case "$1" in
'start')
PID=`ps -ef | grep openHAB | grep -v grep | awk '{print $2}'`
if [ "${PID}" != "" ]
then
echo "Oops! Cannot start, openHAB-Screen is already started!"
else
echo "Starting openHAB"
cd ${OHPATH}
sudo -u ${OHUSER} screen -S openHAB -dm sh ./start.sh
#sudo -u ${OHUSER} screen -S openHAB -dm sh ./start.sh #if you want to start OH on debug mode
fi
;;
'stop')
echo "Stopping openHAB"
sudo -u ${OHUSER} screen -S openHAB -p 0 -X stuff "shutdown$(printf \\r)"
sudo -u ${OHUSER} screen -S openHAB -p 0 -X stuff "yes$(printf \\r)"
PID=`ps -ef | grep openHAB | grep -v grep | awk '{print $2}'`
while [ `ps -ef | grep $PID | wc -l` -gt 1 ]
do
echo -n .
sleep 2
done
echo .
;;
'restart'|'force-reload')
echo "Restarting openHAB"
$0 stop
echo "Waiting termination..."
sleep 5s
$0 start
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment