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:
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.
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?
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 ?
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…??
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.
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
var Response =
executeCommandLine(Duration.ofSeconds(5),"C:\\openhab\\conf\\scripts\\test.bat")
I do get an output.txt file on my drive 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?