OpenHAB Shutdown in docker causing container restart

Ref: https://community.openhab.org/t/rest-api-setting-values

I use docker. Originally I had the compose restart condition set to “always”. And when I get into karaf and enter a system:shutdown, I see the container restart. So I tried changing the container defintion to “unless-stopped” and I see the same thing.

2020-01-22T20:01:02.107277018Z 	at su.litvak.chromecast.api.v2.ChromeCast.getMediaStatus(ChromeCast.java:295)
2020-01-22T20:01:02.107295192Z 	at org.openhab.binding.chromecast.internal.ChromecastCommander.handleRefresh(ChromecastCommander.java:102)
2020-01-22T20:01:02.107312884Z 	at org.openhab.binding.chromecast.handler.ChromecastHandler$Coordinator$1.run(ChromecastHandler.java:231)
2020-01-22T20:01:02.107415805Z 	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
2020-01-22T20:01:02.107436542Z 	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
2020-01-22T20:01:02.107453121Z 	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
2020-01-22T20:01:02.107551048Z 	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
2020-01-22T20:01:02.107579133Z 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2020-01-22T20:01:02.107666852Z 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2020-01-22T20:01:02.107685342Z 	at java.lang.Thread.run(Thread.java:748)
2020-01-22T20:01:35.307801819Z ++ test -t 0
2020-01-22T20:01:35.307871531Z ++ echo false
2020-01-22T20:01:35.307992396Z + interactive=false
2020-01-22T20:01:35.308042896Z + set -euo pipefail
2020-01-22T20:01:35.308058712Z + IFS='
2020-01-22T20:01:35.308148694Z 	'
2020-01-22T20:01:35.308257131Z + '[' limited = unlimited ']'
2020-01-22T20:01:35.308453801Z + rm -f /openhab/runtime/instances/instance.properties
2020-01-22T20:01:35.312066014Z + rm -f /openhab/userdata/tmp/instances/instance.properties
2020-01-22T20:01:35.314698782Z + NEW_USER_ID=1000
2020-01-22T20:01:35.314736099Z + NEW_GROUP_ID=1001
2020-01-22T20:01:35.314782122Z + echo 'Starting with openhab user id: 1000 and group id: 1001'
2020-01-22T20:01:35.314931527Z Starting with openhab user id: 1000 and group id: 1001
2020-01-22T20:01:35.315331139Z + id -u openhab
2020-01-22T20:01:35.321994005Z + case ${OPENHAB_VERSION} in
2020-01-22T20:01:35.322965095Z ++ ls -A /openhab/userdata
2020-01-22T20:01:35.326520191Z + '[' -z 'cache
2020-01-22T20:01:35.326573595Z config
2020-01-22T20:01:35.326595058Z etc
2020-01-22T20:01:35.326611416Z jsondb
2020-01-22T20:01:35.326627833Z logs
2020-01-22T20:01:35.326649871Z persistence
2020-01-22T20:01:35.326668405Z temp
2020-01-22T20:01:35.326683671Z tmp
2020-01-22T20:01:35.326700113Z zwave' ']'
2020-01-22T20:01:35.327729180Z ++ cmp /openhab/userdata/etc/version.properties /openhab/userdata.dist/etc/version.properties
2020-01-22T20:01:35.330909818Z + '[' '!' -z ']'
2020-01-22T20:01:35.331991416Z ++ ls -A /openhab/conf
2020-01-22T20:01:35.336149738Z + '[' -z 'a.out
2020-01-22T20:01:35.336199572Z .git
2020-01-22T20:01:35.336211954Z .gitattributes
2020-01-22T20:01:35.336223732Z html
2020-01-22T20:01:35.336234812Z icons
2020-01-22T20:01:35.336246043Z items
2020-01-22T20:01:35.336257008Z persistence
2020-01-22T20:01:35.336268378Z README.txt
2020-01-22T20:01:35.336279761Z rules
2020-01-22T20:01:35.336290544Z scripts
2020-01-22T20:01:35.336301455Z services
2020-01-22T20:01:35.336312443Z sitemaps
2020-01-22T20:01:35.336323094Z sounds
2020-01-22T20:01:35.336333785Z things
2020-01-22T20:01:35.336346517Z transform' ']'
2020-01-22T20:01:35.336409994Z + chown -R openhab:openhab /openhab
2020-01-22T20:01:35.470491001Z + sync
2020-01-22T20:01:35.558968562Z + '[' -d /etc/cont-init.d ']'
2020-01-22T20:01:35.559049583Z + sync
2020-01-22T20:01:35.611494739Z + '[' false == false ']'
2020-01-22T20:01:35.612797451Z ++ IFS=' '
2020-01-22T20:01:35.612867658Z ++ echo gosu openhab tini -s ./start.sh
2020-01-22T20:01:35.613775429Z + '[' 'gosu openhab tini -s ./start.sh' == 'gosu openhab tini -s ./start.sh' ']'
2020-01-22T20:01:35.613806891Z + command=($@ server)
2020-01-22T20:01:35.613999519Z + exec gosu openhab tini -s ./start.sh server
2020-01-22T20:01:35.626036214Z Launching the openHAB runtime...

The errors are a disconnection, crash from the chromecast as it’s going down…then you see it instantly start recreating the container and starting again.

How can I gracefully stop openhab + the container so that the mapdb is not corrupted?

From the host: docker stop openhab2

This will issue a signal -HUP to the openHAB process and it will shutdown gracefully. You can watch it closing down in the logs for verification.

I have my docker run commands set to always restart too, but by issuing the stop using the docker command the container won’t be restarted.

The reason it keeps trying to restart when you close openHAB from the Karaf console is because Docker watches for the main process in the container to exit and if that happens it restarts it.

1 Like

Ahh so I can just stop the container? I remember when I had issues with map db previously I was told that it was because I was doing this (or restarting the server - which should be stopping the container first - hmm I’d have to investigate that latter point).

Is there the opposite of system started event that I could write a rule in for a notification for testing?

Found it:

rule "System Shutdown"
when
        System shuts down
then
        logInfo("System Shutdown","System is going down")
        g_switchpersist.allMembers.forEach[i |
                logInfo("System Shutdown","g_switchpersist: " + i.name + " State: " + i.state.toString) ]
end

Honestly, there was a time years ago when I was told that the System shuts down trigger didn’t work or didn’t always work. I’ve never had opportunity to test it since. I hope it works for you.

When using Scripted Automation, there is a scriptUnloaded() function you can define which will get called when that particular file get’s unloaded, which does appear to occur when OH is stopping nicely.