Help with EXEC binding when running OH as service on Windows

Hey, guys.

I’m running openHAB 2.5.5 as service on Windows 10.

I have a simple rule:

rule "Shutdown Server"
when
    Item System_Shutdown received command ON
then
    executeCommandLine("C:\\openHAB2\\conf\\bat\\shutdown.bat", 1000)
end

And the content of my shutdown.bat file is also pretty simple (for test purposes):

notepad.exe

I’ve been trying to make the EXEC binding work for quite a while and that what I’ve found:
if i start OH with the start.bat script, everything works and notepad window appears.
if i start OH as service, nothing happens and the log looks like this:

14:40:04.225 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'System_Shutdown' received command ON
14:40:04.240 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'C:\openHAB2\conf\bat\shutdown.bat'
14:40:04.303 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - exit code '1', result '
C:\openHAB2\userdata>notepad.exe
"notepad.exe" is not recognized as an internal  or external command, operable program or batch file.'

So i’ve tried to start service not with SYSTEM, but my USER account, but the behavior stays the same.
I’ve also tried to add runas command to my batch file, but i get the same behavior.

So my question is how to make OH execute batch files when running as service?
I know this might not be an OH problem, but still maybe somebody knows what to try. I’ll appreciate any help.

Have you tried to use the full path to the exe file in the batch file ?
Alternatively you may check if the PATH variable includes the path where the executable is located.
But I am not sure - you may try it - if a program that requires a display can be executed.

Yes, I’ve tried using the full path. Also checked the PATH variable, the path to notepad.exe is listed there.
The batch file is executed successfully when running OH not as service.

I don’t think that is allowed

Services run in session 0 which is isolated from the interactive desktop. You cannot show UI from a service.

Good point, thanks!
i’ve tried some non UI commands, and get the same error:

18:21:51.259 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'C:\openHAB2\conf\bat\shutdown.bat'
18:21:51.319 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - exit code '1', result '
C:\openHAB2\userdata>ipconfig /all
"ipconfig" is not recognized as an internal  or external command, operable program or batch file.'


18:23:45.037 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'C:\openHAB2\conf\bat\shutdown.bat'
18:23:45.100 [DEBUG] [clipse.smarthome.io.net.exec.ExecUtil] - exit code '1', result '
C:\openHAB2\userdata>shutdown /r
"shutdown" is not recognized as an internal  or external command, operable program or batch file.'

Have you also tried ipconfig with path ?
What about a simple

echo "1 2 3" > c:\tmp\test.txt
1 Like

You are genius, thank you!

echo "1 2 3" > c:\tmp\test.txt

works just fine, but ipconfig command requires full path. So

C:\Windows\System32\ipconfig.exe /all

works just fine.

I don’t know, why SYSTEM account in Windows doesn’t see system variables path.

Want to bring this topic up again, because I have an issue running a command from OH3 under Windows 10 (OH3 runs as a Service). The rule I have should turn on the monitor in the morning from standby, when a motion detector becomes active. Here is the command I want to use for this:

var result = executeCommandLine(Duration.ofSeconds(10), "C:\\Tools\\NirCmd\\nircmdc.exe", "monitor", "on")
logInfo("GoodMorning", "EXE monitor on - {}", result)

The result in the log file is simply this (nothing):

2021-09-01 16:26:18.126 [INFO ] [penhab.core.model.script.GoodMorning] - EXE monitor on -

I tried any combination of “/” and “\” in the path, also added commands in the exec.whitelist, but it does not work.

Any idea out there?

You won’t fix it with random combinations.

Whitelist is not related to executeCommandLine at all.

Most likely your utility can’t access something from the service environment.

Try a simple test like ipconfig from above to satisfy yourself you are using the rule action correctly.

Thank you for your reply. Whitelist was neccessary in OH2 for executeCommandLine as far as I know. Is this not valid in OH3 any more?

Was also one of my assumption that the nircmd tool simply does not work when called from a service…

no.
whitelisting is required for the exec binding.
Not for executeCommandLine which is part of OH core actions.

I am running openHab 3.4 on Windows 11 (as a service)

I am trying to restart a binding from the gui by using the exec binding.
The command I use works perfectly fine when I open a command prompt, but not when I use the exec binding. The error I get is:

Passing to shell for parsing command.
OS: WINDOWS (Windows 10)
Exec [ERROR]: ‘The filename, directory name, or volume label syntax is incorrect.’

The command I try to execute is:

“C:\Openhab\runtime\bin\client.bat”,“-p”,“habopen”,“bundle:restart org.openhab.binding.mqtt.espmilighthub”"

Is there any way to do something like this on Windows?

Use double backslashes instead of single backslashes.
I also would put bundle:restart and org.openhab.binding.mqtt.espmilighthub into their separate pair of double quotes delimited by comma.

Thanks for your fast response. The double backslash did the trick, although I don’t understand what it does. Putting the separate pair of quotes around bundle:restart and org.openhab.binding.mqtt.espmilighthub doesn’t seem be be necessary.

1 Like

Windows uses single backslashes as separator un paths. Other OSs and software uses forward slashes. Backslashes are used to escaoe a subset of characters. This is why two of them are required.

Thank you for the explanation!