How to Terminate/Exit/Stop/Quit OpenHAB 3 on Windows from a rule or Exec Binding or Batch File SOLVED

I am running OpenHAB 3 on Windows. Occasionally OpenHAB gets itself in a knot with MQTT and needs to be restarted. I want to automatically / programmatically stop and restart OpenHAB.

I have examined the installed C:\openhab-3.0.2\runtime\bin\stop.bat batch file and find it runs “C:\openhab-3.0.2\runtime\bin\karaf.bat” stop

I created a batch file as follows:
@echo on
ECHO …
ECHO Time is → %date% and %time%
::timeout /t 5 /nobreak
@Pause

“C:\openhab-3.0.2\runtime\bin\karaf.bat” stop

@Pause

“C:\openhab-3.0.2\start.bat”

@Pause

::exit
cmd /k

The “C:\openhab-3.0.2\runtime\bin\karaf.bat” stop instruction successfully terminates Karaf/OpenHAB AFTER a 10 to 60 second delay. I don’t understand why there is a delay, but I can live with it. The delay probably has something to do with a graceful termination?

However, the batch file CRASHES immediately after processing the instruction. This I can not live with as I want to wait for a few seconds, then restart OpenHAB as shown via C:\openhab-3.0.2\start.bat

Am I Terminating / Exiting / Stopping / Quitting OpenHAB 3 inappropriately? Is there a better or best way? I have researched methods but have not found a solution.

Thanks…Any help will be appreciated.

Programs have “parents”. The program that launches another program becomes the parent of that “child” program.

When the parent exits, all the child programs exit too.

So what’s happening is OH launches your .bat script. OH is the parent and the script is the child. The bat script kills OH and because OH is the parent, the child also gets killed before it can get to the part where it starts OH again.

For the most part, you can’t get there from here. You need something outside of OH to stop and restart it. OH cannot do this from a rule. No matter what you try, as soon as OH exits it’s going to kill anything that it launched that is responsible for starting OH back up again.

If the problem is just the MQTT binding, just restart the MQTT bundle. You can do that from a rule becuase you are not killing the whole OH process. You can use executeCommandLine to access the karaf console and restart the MQTT bundle. There are a number of examples on the forum.

Even easier, if it works, would be to just disable then reenable the MQTT Broker Thing. That will force the binding to disconnect and reconned to the MQTT broker. It all depends on what the source of the problem is.

Unfortunately, I don’t remember what JS Scripting looked like in OH 3.0 and whether access to Things was available. But you ought to be able to also disable and reenable the Thing through the karaf console.

In OH 4.0+, using JS Scripting it would look something like this:

var broker = things.getThing("mqtt:broker:<ID>")
broker.setEnabled(false);
setTimeout(() -> broker.setEnabled(true), 10000);

I think the above would even work for a version of OH as early as 3.2 or 3.3.

So, I agree with other post that you may just need to restart the MQTT broker.
However if you want to be able to restart openhab from with it itself you have to do a few extra things.
You might want to do something like this.
I have used this approach and even have a simple widget button on the overview page to trigger the exec command.
a batch file that you have whitelisted for exec to call
lets say we call it try.bat. and it is in the c:\batch folder.
that bat file will simply call another bat file
let’s call it sleep.bat
so all we have in it is this try.bat is this

echo begin automated action
START C:\batch\sleep.bat

Once the new command window is spawned you are now outside of Openhab and the exec call is done and it exits allowing Openhab to be finished monitoring that exec cmd .
The new command window that will manage and monitor your restart activities is fully isolated and outside of any Openhab processes thus can not interfere with it shutting down.
so in your sleep.bat you have something like this.

echo off
echo This command window will print a message in 5 seconds
timeout 5 >nul
echo shutting down openhab
START c:\openhab\stop.bat
echo this command window will print a message in 25 seconds
timeout 25 >nul
TASKKILL /F /FI "WINDOWTITLE eq Karaf"
timeout 2>nul
echo starting up openhab
echo this command window will exit in 5 seconds
timeout 5 >nul
START c:\openhab\start.bat
EXIT

now for your stop.bat you can use something like this in the openhab folder and it should carry over and work as you upgrade versions.

@echo off

echo stopping the openHAB runtime...

setlocal
set DIRNAME=%~dp0%

IF [%OPENHAB_RUNTIME%]==[] (
	set RUNTIME=%DIRNAME%\runtime
) ELSE (
	set RUNTIME=%OPENHAB_RUNTIME%
)
"%RUNTIME%\bin\karaf.bat" stop %*
TIMEOUT 1>nul
EXIT

You can use exec binding to call a bat file this gets you outside of Openhab on separate command window use that window to call your monitor bat file and it can call stop.bat and watch for the clean exit and if it does not it can kill the karaf window with a taskkill then sleep and call your start.bat .
Again, the whole point is you are handling this entire restart process outside of Openhab.
Obviously if you had Openhab running as a service you could use same approach to spawn a external cmd window to call the external bat file and have net stop Openhab net start Openhab in your external bat file.,but that is a whole different story.

Thank you both for your help.

I implemented Justan’s suggestion and it works. I do however get a message I don’t know how to respond to as follows:

Launching the openHAB runtime…
There is a Root instance already running with name openhab and pid 800. If you know what you are doing and want to force the run anyway, SET CHECK_ROOT_INSTANCE_RUNNING=false and re run the command.

UPDATE: Actually it sort of works. The restart gets stalled / hung up at:

Launching the openHAB runtime…
There is a Root instance already running with name openhab and pid 10076. If you know what you are doing and want to force the run anyway, SET CHECK_ROOT_INSTANCE_RUNNING=false and re run the command.

The only reason that would display is if the first Openhab instance had not stopped and you attempt to start a second instance.
This approach is based on you running OpenHab from the start.bat and the Karaf command window is visible on the desktop.
If you are running OpenHab as a windows service or from a scheduled task in the background you will need to approach this differently
Edit: one other question is are you really running OpenHab version 3.0?
I have used this on versions 3.1 and all higher versions at some point all the way up to 4.2.1 and it has always stopped the first instance left me an open Karaf command window that I taskkill and then start a fresh instance.
If your OpenHab is not shutting down correctly you may have other issues that are more than what you have observed or mentioned.

I am running OpenHab from the start.bat and the Karaf command window is visible on the desktop.
I have increased the the delay after the
START c:\openhab-3.0.2\runtime\bin\stop.bat
command to 60 seconds and all appears well now. I’ll continue to test

Hi,
Glad you got it working .
You could make your script more intelligent and use TASKLIST /M and use a :loop statement to watch for the java.exe process to exit then move to next action but that just seems like a lot of work to just save a few seconds on the restart but it may helpful if your duration of stop time for shutting Openhab down is varying significantly when that bad condition you are restarting for occurs.

For the benefit of others desiring to programmatically stop and restart OpenHAB, and to publicly document my solution, here is my code:

Items:
Switch IP_HS5150_RestartOH “IP_HS5150_RestartOH [%s]” {channel=“exec:command:ExecRestartOH:run”, autoupdate=“false”}

Things:
Thing exec:command:ExecRestartOH [command=“C:\openhab-3.0.2\ReStartOpenHAB\restartOH”, interval=0, timeout=5, autorun=false]

Rules:
IP_HS5150_RestartOH.sendCommand(ON)

Whitelist:
C:\openhab-3.0.2\ReStartOpenHAB\restartOH

restartOH.bat Batch File:
@echo on
ECHO …
echo Begin Automated Action
timeout /t 2 /nobreak
START C:\openhab-3.0.2\ReStartOpenHAB\restart.bat
EXIT
cmd /k

restart.bat Batch File:
echo off

echo Shutting down OpenHAB
START c:\openhab-3.0.2\runtime\bin\stop.bat

echo Allow some time for OpenHAB to complete and stop
timeout /t 60 /nobreak

echo Killing Karaf
TASKKILL /F /FI “WINDOWTITLE eq Karaf”

echo Allow some time for Karaf to die
timeout /t 5 /nobreak

echo Starting OpenHAB
START c:\openhab-3.0.2\start.bat

echo Allow some time for OpenHAB to read the computer screen
timeout /t 5 /nobreak

EXIT
cmd /k