[Solved] Docker - How to recycle openhab within the docker container?

OpenHAB: V2.5.-M1-amd64-debian
Docker on x86 Synology NAS …

Hi there,
I have timing issues from time to time for my zWave stick to bring the sensors online before OpenHAB boots in my docker container (which is started at startup of my NAS box). In this case I see in the karaf console log that the zWave sensors are still offline and hence all the rules and events go wrong. I send myself emails to check the sensor status and at the moment I solve this by manually restarting the container when getting emails with offline status and after reboot everything works. And this only happens from time to time.

The offline state of the sensors gets only determined by openHAB and hence I would just like to recycle openHAB within the container in that case … because the docker commands on my NAS need root privileges and I do not want to let openHAB to get root credentials to restart the whole docker container from the NAS command line!

I would prefer to simply recycle openHAB within the container to see if this already solves the problem but I did not find a way how to do that because the Linux shutdown and reboot command do not exist in the container and do not feel appropriate. And I did not find any other way to recycle openHAB within the container??? Is there a way to reinitialize openHAB within the container probably via a karaf console command or any other appropriate way.

Does someone has any suggestions for me how to accomplish that?

Cheers
Justus

You should have a group called “docker” or similiar on your NAS. Put the user openhab into that group, and presto, you can run "docker stop " from the command line.

About the shutting down question, yes, there should be no “shutdown” command inside the container. It simply stops when the main process inside the container stops.

If you set up your container with “restart: always” ( see https://docs.docker.com/config/containers/start-containers-automatically/ ), you can simply stop the container and wait until it comes up by itself again :smile:

Hello Hakan,

thanks for your quick reply. I do not have a group called “docker” on my NAS … Docker is installed via an official synology app and obviously it is directly linked to the administrators.
I have thought about manually “patching” the docker privileges on my NAS but this may not survive any future NAS or Docker app updates … hence I was looking for a method within the container … I will think about our idea with adding/changing credentials for docker administration on the NAS but it interferes with the default at the moment …

Yep, but be aware that this would leave the “docker way of things”, opening up many more problems down the road for you.

What you would need is a way to reinitialize the java process inside the container. Which you cannot do by simply killing it, as this would terminate the container itself. Also, as the openHAB process is running inside the container, it has no (easy) way to tell the outside world that it wants to be killed / restarted.

And would the container restart then if it is set to “always restart”?

I do not want to tell anyone outside … I just simply want openHAB to restart itself when it detects in a rule that the sensor is offline. Something like

rule “Restart when zwave sensor stays offline”
when
Item sensor_status changed
then
if ( sensor_status == “OFFLINE”)
{
# run a script to kill the java process so that the container terminates and restarts itself
}
end

This is just quick and dirty idea how it should work, demonstrating that I do not want anything to the outside world.

Would this work?

Yes, this is one way to do it. The script would run inside the container, and can do thus something like “killall -9 java” :grin:

Thus, the container would terminate and go away, and shortly afterwards the docker runtime would restart a fresh container.

Cool! Thanks … I would try that on the weekend if killall would exist :frowning:
That would have been ideal to recycle in the rare case of the timing issues with the sensor!!!
Need to find out what is available to kill the container from the inside …

I found that killall5 is available on the container and I tried it manually and the docker container restarted … Now I am figuring out the rule around it!

Thanks for your support!!! Seems that I am almost there :wink:

Hello Hakan,

I have built the test rule below to initiate a killall … killall5 is located in /sbin/killall5

root@openhab-2:/openhab# ls -l /sbin/killall5 
-rwxr-xr-x 1 root root 23224 Feb 12  2017 /sbin/killall5
root@openhab-2:/openhab# 

It is executable for all users!!!
I can run it from command line as user root and user openhab and the container stops …

But my test rule (see below) does not work ???

Do you have any advice what I am doing wrong?

rule "Do what I want"
  when
    Item test changed to 2
  then
    logInfo("FILE", "***** Entering: DO WHAT I WANT: " + test.state)

    logInfo("FILE", "***** BEFORE ... ")

    val results = executeCommandLine("/sbin/killall5", 5000)
    logInfo("FILE", results)
    // executeCommandLine("/sbin/killall5")

    logInfo("FILE", "***** AFTER ... ")

end

Here is what I get in the log when activating log:set debug org.eclipse.smarthome.io.net.exec

19:03:35.449 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/sbin/killall5'
19:03:35.500 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - exit code '0', result ''

Mmmmhhh,

maybe that is the reason why:

killall5 is the SystemV killall command. It sends a signal to all processes except the processes in its own session, so it won’t kill the shell that is running the script it was called from. Its primary (only) use is in the rc scripts found in the /etc/init.d directory.

I changed the rule to:

rule "Do what I want"
  when
    Item test changed to 2
  then
    logInfo("FILE", "***** Entering: DO WHAT I WANT: " + test.state)

    logInfo("FILE", "***** BEFORE ... ")

    val results = executeCommandLine("/openhab/recycle.sh", 5000)
    logInfo("FILE", results)
  
    logInfo("FILE", "***** AFTER ... ")

end

and put the following command into that script /openhab/recycle.sh :

#!/bin/sh
kill -9 -1

This kills my docker container and it restarts because of the Restart-Policy: alyways

1 Like