openHab2 startup chaos

Thursday, October 25. 2018

starting and stopping openhab2 is a messy affair, filling the logs with loads of Java's verbose exception messages and since the order of loaded models is basically random things may or may not work as intended when items have not been loaded or rules not executed that are assumed to be there.

As it was manual corrections and reloads were needed after each restart.

There is an elegant workaround discussed at which uses systemd's

ExecStartPre and ExecStartPost commands to deactivate all rules before starting openhab intially, and then reactivate them one by one when the system is up and has it's feet on the ground. a small bash script does the renaming. Since the openhab2.service file is part of the package and thus is replaced with each update modifying the .service file would need to be repeated whenever a new version gets installed, but, systemd provides a way to override a service definition. systemctl edit openhab2.service creates an override dir and opens the editor to allow creating service file that is not replaced by the next update. Since the bash script works the list of rules -files in an alphanumerical order a naming scheme like praefixing all .rules with a three digit number finally allows to control the order of rules loading.


The bash script sits at some place like /etc/openhab2/exec-scripts/and looks like:



# clean up the start process 
#  starting rules in a sorted manner after openhab2 got it's feet on the ground
#  called from systemd pre and post start 
#  to rename *.rules away initially and then rename them back one by one
#  /etc/systemd/system/openhab2.service.d/.#override.conf041cd66becaa4539
# $3 allows to distinguish between pre and post action for other THings to eXecute
# REF:


for f in /etc/openhab2/rules/*.${ORG};
    CURRENT=$(basename $f)
    if [ "$CURRENT" == "$IGNORE" ]    
        echo "ignoring $IGNORE"
        mv "$OLDFILE" "$NEWFILE"
    # let some time between each load
    if [ "$THX" == "POST" ]
        /bin/sleep $DUR

# some things left on startup
if [ "$THX" == "POST" ]
    /usr/bin/touch /etc/openhab2/tradfri.things


The relevant lines in the [Service] part of the openhab2.service file are:


ExecStartPre=/etc/openhab2/exec-scripts/ rules rules_ 
ExecStart=/usr/share/openhab2/runtime/bin/karaf $OPENHAB_STARTMODE 
ExecStop=/usr/share/openhab2/runtime/bin/karaf stop 
ExecStartPost=-/bin/sleep 90 
ExecStartPost=/etc/openhab2/exec-scripts/ rules_ rules POST 


(The lines with ExecStart / ExecStop are already there.)


Trackback specific URI for this entry
    No Trackbacks


    No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.

    Submitted comments will be subject to moderation before being displayed.