executeCommandLine does not run batch file

Hello
I am running on Win 11 and openHAB version: 3.4.2 into following problem:

I do have following rule created:

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: logInfo("TEST", "Start Batchfile")
        executeCommandLine("C:\\Windows\\System32\\cmd.exe","/c","C:\\openhab\\conf\\scripts\\test.bat")
        logInfo("TEST", "End Batchfile")
    type: script.ScriptAction

in the test.bat file I have following line: **"C:\Windows\system32\calc.exe"**

When triggering the rule manually via the UI I get following log:

2023-02-18 08:35:21.878 [INFO ] [org.openhab.core.model.script.TEST ] - Start Batchfile
2023-02-18 08:35:21.883 [INFO ] [org.openhab.core.model.script.TEST ] - End Batchfile

but the calc.exe never opens! Is there anybody who can give me a hint why it is not working?

Thanks!!!

  • use the version of executeCommandLine that waits for the command to be executed.
    Extract from the docs:

executeCommandLine(Duration.ofSeconds(timeout), String commandLine): Executes a command on the command and waits timeout seconds for the command to complete, returning the output from the command as a String. For example you could run var ScriptResponse = executeCommandLine(Duration.ofSeconds(60), "path/to/my/script.sh"); would get executed and wait 1 minute for the output to be responded back and write it into the ScriptResponse variable.

  • log the content of ScriptResponse variable to check for returned messages
  • Besides that calc.exe is a program that requires access to your windows desktop to be displayed ? In case you run openhab as a service that may be a problem as the program ( calc.exe ) then will be started with different user permissions ( openhab ) than the logged in desktop ( your user ) is running with.

Hello
thanks for your response. I changed the rule like this:

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: logInfo("TEST", "Start Batchfile") ScriptResponse =
        executeCommandLine(Duration.ofSeconds(10),
        "C:\\Windows\\System32\\cmd.exe","/c","C:\\openhab\\conf\\scripts\\test.bat")
        logInfo("TEST", "ScriptResponse=" ScriptResponse) logInfo("TEST", "End
        Batchfile")
    type: script.ScriptAction

the log shows:

Script execution of rule with UID '5e0474553a' failed: logInfo("TEST", "Start Batchfile") ScriptResponse = executeCommandLine(Duration.ofSeconds(10), "C:\\Windows\\System32\\cmd.exe","/c","C:\\openhab\\conf\\scripts\\test.bat") logInfo("TEST", "ScriptResponse=" ScriptResponse) logInfo("TEST", "End Batchfile")

It does not matter if I use a timeout with 10s or 60s… as well as the user and openhab should have both admin rights. The batch file I can run without any issue and I also do not get any window or error message displayed about the rights permission.

Do you see any other problems and/or how can I better debugg this issue?

Thanks in advance

Isn’t there a special permission in windows for launching a GUI program from a service (if that’s even possible/allowed)? It’s not an openhab specific issue.

Instead of running calc.exe from with in the batch start a batch file that e.g. writes out a debug message to a file or just touches a file and you can examine the timestamp of the file if it was touched.
Once that is working check again if you can get calc.exe running.

What is your use case ? Would you like to open calc.exe at the end or would you like to replace it with something else once it is working ?
Is your openhab instance running as a service ?
Did you check if the eventviewer contains a hint why calc.exe wasn’t started ?

  • openhab is running as a service
  • there is nothing written in the events.log which is linked to the batch file or calc.exe

instead of the calc.exe I simply put an “exit 1” to the batch file in order to receive a return value. in the openhab.log I do not see the value of the return value as well there is not error message logged in the file events.log.

I used the calc.exe only for a simple check. When the rule is working - I hope - then I would the batch file to run some commands to control an attached device.

I stopped the service and started the batch file “start_debug” and manually triggered the rule again. In the events.log I could not see anything as well there was the same entry in the openhab.log file.

Now I am quite lost and have no idea how to check further. Any ideas…??

Thanks

I would try to execute “dir”. In any case that should be executed.
Also try to escape the parameter. Replace “/c” by “\/c”

I do not use GUI managed rules,but aren’t there line breaks missing after each command or is it just copy/past error?

1 Like

calling dir in a .bat file and redirect the output to a file in temp folder would be an other test

… if one can make 100% sure that the user has write rights to that directory. Anyway, at least we should get back an error message from cmd.

I changed it according your input the rule like this and triggered again the rule with following result:

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '5e0474553a' failed: logInfo("TEST", "Start Batchfile")
ScriptResponse = executeCommandLine(Duration.ofSeconds(10),"C:\\Windows\\System32\\cmd.exe","\/c","C:\\openhab\\conf\\scripts\\test.bat")

in the batch file I simply wrote:

dir

pause
exit 123

When I run direct this batch file, the cmd window opens and remains open until I press a button. But with the rule it does not work.

You mentioned I should not use the GUI for the rules?

On the directory “files” where the batch file is stored I do have full access (write and read).

The remark of @Oliver2 was meant with regard to storing the output of the command in the batch file into a file in the filesystem during runtime e.g.

dir > c:\temp\myoutput.txt

or

dir > c:\tmp\output.txt

depending on which on exists on windows system ( as windows is not my first love I always forget about that part ).

May I kindly ask why you added

pause

and

exit 123

Pause as you explained will output the list of directories and files and afterwards waits for a keystroke. As this will run in the background ( and with openhab permissions ) I expect that you will not get the chance to press any key. If you do not get that window executeComandLine will wait for the timeout and end the command. I think it will not reach the exit 123 part. And even that in case it would be reached isn’t something like an error code ( at least under linux everthing differnt to exit 0 is an error that is returned ).

I am more concerned about this one. I think OH is interpreting two lines of code as just one line code.
As said, please add new lines after each command.

And I agree with Wolfgang. Pause is counterproductive. I would not even divert the output. Please also add dir in the second line of your batch file.

well I thought I could use the “pause” in order to see if the batch file is opening and remains open, which was not the case. The “exit 123” I tried to get an error code written to the variable defined in the rule but it was also not working…

Ok now I changed the rule and the batch file as you recommended this way:

batch file:

dir > d:\output.txt

I used the “d:” to make sure this drive has no permission limitations. When manually triggered the batch file I get the expected “output.txt” file with the dir folder in it.

OH rule:

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: Response =
        executeCommandLine(Duration.ofSeconds(5),"C:\Windows\System32\cmd.exe","\/c","C:\openhab\conf\scripts\test.bat")
    type: script.ScriptAction

Here I tried with double “\” as well but I always get the same log entry as below:

Script execution of rule with UID '5e0474553a' failed: Response = executeCommandLine(Duration.ofSeconds(10),"C:\Windows\System32\cmd.exe","\/c","C:\openhab\conf\scripts\\test.bat")
2023-02-25 08:19:13.714 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '5e0474553a' failed: Response = executeCommandLine(Duration.ofSeconds(5),"C:\\Windows\\System32\\cmd.exe","\/c","C:\\openhab\\conf\\scripts\\test.bat")
2023-02-25 08:21:12.224 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '5e0474553a' failed: Response = executeCommandLine(Duration.ofSeconds(5),"C:\Windows\System32\cmd.exe","\/c","C:\openhab\conf\scripts\test.bat")


Is it possible that you can use exactly my rule in your system and check if you run into the same issue? I have really no idea why this command is not working at all!

I believe that the problem is not the batch file but rather the rule or the executeCommandLine cmd. Is there an "easy" way to check if this cmd is working?

Thanks

when opening another rule I see this syntax:

script: >-

When I change it that way, save it and open the rule again, it has been deleted again… So I am not sure if it is necessary or not.

try the following:

var Response = executeCommandLine(Duration.ofSeconds(5),"notepad.exe")
var Response = executeCommandLine(Duration.ofSeconds(2),"cmd.exe","/c","dir")

with the first line only (notepad.exe) I get following log while notepad.exe is not opening:

 Timeout occurred when executing commandLine '[notepad.exe]'

with second line (cmd.exe) I do not get any log at all. It also does not open the cmd.exe

but when using this:

var Response =
        executeCommandLine(Duration.ofSeconds(5),"C:\\openhab\\conf\\scripts\\test.bat")

I do get an output.txt file on my drive :slight_smile: for my batch file

dir > d:\output.txt

Now I would like to run this batch file:

powershell -command "D:\test.ps1"

pause

When doing so, I get again the “timeout log entry”. This means for some reasons the powershell script is not opening.

Do you have an idea in which way I need to change the batch file to call a powershell script? Or is it possible to call directly the powershell script?