Trigger Batch from Rule on Windows


I am running OpenHab on a Windows 10 machine. So everything is working and my rules are also working.

I’d like to create an item to restart the OpenHab service. I have already a working batch which can be used manually on the device.

I prepared everything and tried to run a simple batch file from a rule. I tried a command like this:

val String results = executeCommandLine("C:\\Temp\\test.bat", 100)
logInfo("TEST", "Skript" + results)

I can see that the rule is triggered but in the Log it only shows “Skript” without anything else. I tried to trigger a batch or an executable without success.

Anyone can help me with this?

What version of openHAB? If 2.5.2 did you create the whitelist as stated in the Release Notes?

Nop, running 2.5.0…

I don’t think the whitelist is (currently) used for exec Action anyway.

That’s normal enough in itself. Depends what the mystery batch does.

This is made difficult because the process that you want to restart openHAB will be running under openHAB.
Maybe it’s possible to use Windows Scheduler.

Already have the scheduler which is restarting every night. Last week I had an issue and my wife was not able to restart so I am searching for an easy way that she can do in future.

So no chance to run a batch which is tagged with the admin credentials?

Yes, I run batch under Windows from executeCommandLine just fine.

Simple test to demonstrate it works
If that pops up a notepad, you’ll know your problems are something about your context.

My circumvention about scheduler was to schedule a one-time job in, say, one minutes time.

Already tried this with notepad also with the full path but nothing happens!

Notepad does not start on my windows machine if I trigger the related rule!

Works for me. No point in trying anything else until you get that to work.
Are you running openHAB as a service? I don’t know if that makes any difference.

Yes, OH is running as a service. How can I troubleshoot that because there is nothing in the log?! I can see that my rule is triggered but notepad won’t start.

I really don’t know. It just works for me (but I don’t run as a service).
It certainly feels like some kind of permissions issue.

Anything in Event Viewer?

Try turning on logging for executeCommandLine, …

log:set DEBUG

… increasing the timeout, and using parameterized logging…

val String results = executeCommandLine("C:\\Temp\\test.bat", 5000)
logInfo("TEST", "Skript: {}", results)

Here is a simple test.

Batch file testbat.bat - placed in /scripts/ so we have local permissions

echo rhubarb

rule fragment, trigger as convenient. Note full path given, with escaped \.

var results = executeCommandLine("C:\\openhab2\\conf\\scripts\\testbat.bat", 1000)
logInfo("exectest","got " + results)


2020-02-27 11:33:04.608 [INFO ] [ipse.smarthome.model.script.exectest] - got 
C:\openhab2\userdata>echo rhubarb 

newlines from output, not me.

Unlike notepad test, we’re not trying to open a new window or get to anyone else’s folders…

1 Like

will test, keep you posted

Ok, tested. Now I have some information in the log:

2020-02-27 17:35:42.366 [DEBUG] [] - executed commandLine 
2020-02-27 17:35:42.519 [DEBUG] [] - exit code '0', result '
C:\Openhab\userdata>start notepad.exe '
2020-02-27 17:35:42.519 [INFO ] [ipse.smarthome.model.script.exectest] - got 
C:\Openhab\userdata>start notepad.exe 

The batch can be started manually without problems and notepad will start directly.

Try using cmd to start notepad?
cmd /C notepad.exe might work.

We are getting WAY off-topic into Windows functions though.

sure…this command is working. My test batch includes

start notepad.exe

which is also working if I start the batch manually. It is only not working if the rule is triggering the batch

Yes, but you will find a DOS command like del filename cannot be executed directly from executeCommandLine - because del is not a program, you are not typing into a dos box here. You would have to use cmd /c del filename

But as said, off topic.
notepad.exe is a program file and should run.
Something in your setup is preventing it opening a new window.
What timeout are you using in your test? 100mS is not long to fire up notepad.

Did you try my echo test at all?


sorry for the late response and thanks for your help. The echo test is working. But if I try to use “cmd /c notepad.exe” or cmd /c “full path to notepad.exe” then I just got the message, that this command can’t be found.

How can I solve to open an application? Any more hints?


The appropriate test is just
It’s not a part of cmd.exe

The echo test works. So you know executeCommandLine() works.

You don’t yet seem to have verified if you are actually running your bat. Maybe add an echo to your bat before your suspect notepad line, and find out.

I still suspect you have some kind of permissions issue preventing a bat run from openhab user opening a new window. But you don’t seem to have thought about timeout either.

Narrow down the problem.