Starting OPENHAB2 automatically with boot sequence

Hello together I have followed the instructions of Martin to install openhab2 on the AXC F 2152. Anything’s work fine, but issue, that openhab won’t start automatically after a reboot is a little bit annoying. That forces me to change it, and here is the solution. 1) Because of the fact, that standard text files couldn’t be uploaded (-> hopefully the admins will change that soon after they have eliminated the worm hole) I have attached the sources of the script behind this article. You have to copy & paste it and store it inside a file named "openhab2 " (TAKE CARE ABOUT, THAT IT HAS TO BE STORED FOR LINUX SYSTEMS) With notepad++, you can configure it under /settings/Preferences/NewDocuments → Change it to UNIX…then the string termination will be LF 2) After that, you have to copy this file to the PLC inside the folder "/etc/init.d " (Login as root, otherwise it causes problems) 3) Open a terminal session and login as root user 4) Type in following command "update-rc.d openhab2 defaults 80 10 " 5) Be happy From that point, openhab2 will start automatically after boot. You can run the script as root as follows : ./openhab2 (start | stop | restart) ##################################################### Script Section ################################################### #!/bin/sh

BEGIN INIT INFO

Provides: Start | Stop | Restart of openhab2

Required-Start: network ha sto be started

Required-Stop: Was muss vorher beendet werden?

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Script to start, stop and restart openhab

Description: The script starts openhab normally. Stopping openhab is

realised by scanning for process Ids and first kiling the karaf console

and then the main process (this may take a while)

END INIT INFO

Author: Michael Gulsch # Aktionen umask 077 scan_process(){

scan_result=ps | grep $1
scan_result=$(echo ${scan_result%%root*})
scan_result=$(echo ${scan_result%% })
space=
scan_result=$space$scan_result
if [ $scan_result -eq 0 ] | [ $scan_result -ne $1 ];
then
echo “0”
else
echo “1”
fi
}
start(){
/opt/openhab2/start.sh server &
echo “Openhab2 - Service started”
} stop(){
grep_result=ps | grep {karaf}
grep_result=$(echo ${grep_result%%root
})
grep_result=$(echo ${grep_result%% })
space=
grep_result=$space$grep_result
kill $grep_result
scan=“1”
echo "Openhab2 - started to kill “karaf” terminal: " $grep_result
while [[ $scan -eq 1 ]]; do
scan=scan_process $grep_result
sleep 1
echo -n “.”
done
echo
echo “Openhab2 - Terminal terminated”
grep_result=ps | grep Dopenhab
grep_result=$(echo ${grep_result%%root
})
grep_result=$(echo ${grep_result%% *})
grep_result=$space$grep_result
kill $grep_result
scan=“1”
echo "Openhab2 - started to kill main process: " $grep_result
while [[ $scan -eq 1 ]]; do
scan=scan_process $grep_result
sleep 1
echo -n “.”
done
echo
echo “Openhab2 - Main process terminated”
} restart(){
stop && start
} case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo “use: openhab2 {start|stop|restart}”
exit 1
;;
esac exit $?

Very nice, thank you for posting this exkalibur28. Out if interest, how are you using OpenHab with the AXC F 2152? Have you tried driving I/O from the controller and, if successful, how did you do this? (we had discussed options like Modbus previously, but the best option would be for someone with the time and inclination to write a custom PLCnext “binding” for OpenHab.) - Martin

Hi Martin, you can call me Michael…(my Name is visible under Author of the script above). Today I use the NODE RED and OPENHAB example as demonstrator in sales trainings. Regarding the binding, I think the best way would be, that someone write an OPC UA bindung for OPENHAB. That is one thing that is in discussion in their community, NODE-RED has one, and it is a fun how easy it is, to connect NODE-RED with PLCnext over OPC UA. - Michael

Hi Michael, Im trying to connect PLCnext Vars via nodered (node-red-contrib-iiot-opcua, node-red-contrib-openhab-v2) modules together. I can set some variable from nodered in PLC but not from openhab2 to PLC and vice versa. Can you send me some short description how should be done to connect openhab with PLC ? I thing I have LISTENER solution. 6_DebugFCT_before_after - debug and conversion works OK. OH2 switch item DA16_DA sometime switched OK and sometime does’t switched. What do I wrong please ? Have you some CMD examples for this direction from PLC to OH and from OH back ? Same for measured values ? Thanks for help.



Hello together I have followed the instructions of [Martin</a> to install openhab2 on the AXC F 2152. Anything’s work fine, but issue, that openhab won’t start automatically after a reboot is a little bit annoying. That forces me to change it, and here is the solution. </p>\n1) Because of the fact, that standard text files couldn’t be uploaded (-> hopefully the admins will change that soon after they have eliminated the worm hole) I have attached the sources of the script behind this article. You have to copy & paste it and store it inside a file named “openhab2 </strong>" (TAKE CARE ABOUT, THAT IT HAS TO BE STORED FOR LINUX SYSTEMS) With notepad++, you can configure it under /settings/Preferences/NewDocuments → Change it to UNIX…then the string termination will be LF</p>\n2) After that, you have to copy this file to the PLC inside the folder "/etc/init.d </strong>” (Login as root, otherwise it causes problems)</p>\n3) Open a terminal session and login as root user</p>\n4) Type in following command “**update-rc.d openhab2 defaults 80 10 </strong>”</p>\n5) Be happy</p>\nFrom that point, openhab2 will start automatically after boot. You can run the script as root as follows : ./openhab2 (start | stop | restart)</p>\n##################################################### Script Section ###################################################</p>\n#!/bin/sh

BEGIN INIT INFO

Provides: Start | Stop | Restart of openhab2

Required-Start: network ha sto be started

Required-Stop: Was muss vorher beendet werden?

Default-Start: 2 3 4 5

Default-Stop: 0 1 6

Short-Description: Script to start, stop and restart openhab

Description: The script starts openhab normally. Stopping openhab is

realised by scanning for process Ids and first kiling the karaf console

and then the main process (this may take a while)

END INIT INFO

Author: Michael Gulsch</p>\n# Aktionen</p>\numask 077</p>\nscan_process(){

scan_result=ps | grep $1
scan_result=$(echo ${scan_result%%root*})
scan_result=$(echo ${scan_result%% })
space=
scan_result=$space$scan_result
if [ $scan_result -eq 0 ] | [ $scan_result -ne $1 ];
then
echo “0”
else
echo “1”
fi
}</p>\n
start(){
/opt/openhab2/start.sh server &
echo “Openhab2 - Service started”
}</p>\nstop(){
grep_result=ps | grep {karaf}
grep_result=$(echo ${grep_result%%root
})
grep_result=$(echo ${grep_result%% })
space=
grep_result=$space$grep_result
kill $grep_result
scan=“1”
echo "Openhab2 - started to kill “karaf” terminal: " $grep_result
while [[ $scan -eq 1 ]]; do
scan=scan_process $grep_result
sleep 1
echo -n “.”
done
echo
echo “Openhab2 - Terminal terminated”
grep_result=ps | grep Dopenhab
grep_result=$(echo ${grep_result%%root
})
grep_result=$(echo ${grep_result%% *})
grep_result=$space$grep_result
kill $grep_result
scan=“1”
echo "Openhab2 - started to kill main process: " $grep_result
while [[ $scan -eq 1 ]]; do
scan=scan_process $grep_result
sleep 1
echo -n “.”
done
echo
echo “Openhab2 - Main process terminated”
}</p>\nrestart(){
stop && start
}</p>\ncase “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo “use: openhab2 {start|stop|restart}”
exit 1
;;
esac</p>\nexit $?</p>\n </p>

Hello,
please can you modify your script for Openehab 3 autostart please ?
As root chown
I changed openhab2 → openhab but no fuction:
sh: ./openhab: /bin/sh^M: bad interpreter: No such file or directory
Thanks.
Sorry my mistake was in no switch as unix in notepad++
THANKS.