OH3 Rule for executeCommandLine

Hey there,
I just updated to OH3 and I can‘t get 2 rules working. Other rules are working.

Maybe someone can help?

In OH2 I used a switch to execute something in command line. Unfortunately in OH3 this doesn’t work.

Rule Luftreiniger.rules

rule “luftreiniger einschalten”
when
Item Luftreiniger received command ON
then
executeCommandLine(“/etc/openhab2/rules/luftreiniger_an.sh”,1000)
end

rule “luftreiniger ausschalten”
when
Item Luftreiniger received command OFF
then
executeCommandLine(“/etc/openhab2/rules/luftreiniger_aus.sh”,1000)
end

Script luftreiniger_aus.sh

airctrl --ipaddr 192.168.178.67 --protocol coap --pwr 0

Script luftreiniger_an. sh

airctrl --ipaddr 192.168.178.67 --protocol coap --pwr 1

Please let me know if you need more information or details.

Thank you!
Regards Dirk

execureCommandLine changed in OH 3. I see the docs haven’t been updated for that yet. If I recall correctly, the timeout now is the first argument, the command is the second, and reach argument to the command is a separate argument after that.

The change that @rlkoshak mentioned is one part. You also need to check if the directory is called /etc/openhab2 or if it is called /etc/openhab now.

1 Like

Thank you for the hint. Unfortunately I am not able to “build” this command the right way.
May you show me the correct way for my case?
Thank you!

The directories are both available with the files inside. So both paths should work.

i struggled there yesterday also, but managed to get it to work. My line now looks like:

executeCommandLine(Duration.ofSeconds(5), “/etc/openhab/scripts/rasp_keller_all.sh”)

@spy0r Ah ok, so it should look like this for me ?

executeCommandLine(Duration.ofSeconds(1), “/etc/openhab2/rules/luftreiniger_aus.sh”)

i think so! Try it! Make sure the script is executable as well.

Yeah, you were right. Now my rules are working again :slight_smile:
Thanks

I forgot about the Duration part. I’m glad everyone was able to figure it out.

Hey everyone, just wanted to add something perhaps it saves someone some time, OH3 is only executing my script if I add “bash” like this
var result=executeCommandLine(Duration.ofSeconds(5), “bash”,"/etc/openhab/rules/off.sh")

then the first row does not have the shebang ( Shebang (Unix) - Wikipedia ) which is required to define the script interpreter or execute permission is missing.

Next hint for people, who need to use parameters within their scripts:

This worked with OH2.5.3 in my case:

executeCommandLine("/openhab/conf/scripts/eg01.sh ON",100)

With OH3 the parameter “ON” has also been written in quotation marks:

executeCommandLine(Duration.ofSeconds(1), "bash", "/openhab/conf/scripts/eg01.sh", "ON")

Hope that helps others struggling around.

i have from my 2.5 oh a command line problem with: "

sudo sh /etc/openhab/scripts/BackupCli.sh": error=2, No such file or directory".

so i try to change the syntax with this :

executeCommandLine(“/usr/bin/sudo”,“/etc/openhab/scripts/BackupCli.sh”)
now i haver no error but the scrip don’t run

can you help me on this simple command ? if i run in command line all is perfect

now this work :

executeCommandLine(“/usr/bin/sudo”,“sh”,“/etc/openhab/scripts/BackupCli.sh”)

Now you’re not looking for an error

var results = executeCommandLine(Duration.ofSeconds(5), "/usr/bin/sudo","sh","/etc/openhab/scripts/BackupCli.sh")
logInfo("exec", "exec result - {}", results)

Put debugging in your shell script like creating a file with date timestamp and a message every time it is exeucted. Second have a look to syslog messages to check if there is a message about running sudo.

thx a lot : here is what i get :

Timeout occurred when executing commandLine ‘[/usr/bin/sudo, sh, /etc/openhab/scripts/BackupCli.sh]’

exec result - {}

Execellent, the script started but took more than 5 seconds we allowed for it. That’s not surprising for a backup.
So just allow a longer time now.

thx for your time , i increment the second to “20”

here is the rules:

var results = executeCommandLine(Duration.ofSeconds(20), "/usr/bin/sudo","sh","/etc/openhab/scripts/BackupCli.sh")

 logInfo("exec", "exec result - {}", results)

now in frontail i recieve this : 
2021-03-19 13:00:55.891 [INFO ] [org.openhab.core.model.script.exec  ] - exec result - 

Using '/etc/openhab' as conf folder...

Using '/var/lib/openhab' as userdata folder...

Using '/usr/share/openhab/runtime' as runtime folder...

Writing to '/media/backup/nas/Backup-Openhab-Cli/openhab3_Test-Cli-21_03_19-13_00_46.zip'...

Making Temporary Directory if it is not already there

Using /tmp/openhab/backup as TempDir

                                         

#########################################

          openHAB backup script          

#########################################

                                         

Copying configuration to temporary folder...

Removing unnecessary files...

Zipping folder...

Removing temporary files...

Success! Backup made in /media/backup/nas/Backup-Openhab-Cli/openhab3_Test-Cli-21_03_19-13_00_46.zip

where the "var result " as value " exec result - "

thanks a lot for your time , i don’t ave the skills for that , have you a link or target for learning ? is linux learning ? thera arent many languages whe need to know in openhab , and the more difficult now with the 3.0 is what whe need and where to learn