ExecuteCommandLine in OH3 not working

Hi Helmar,

you have to either replace your space-characters with @, or seperate all of your arguments.
e.g.
executeCommandLine(Duration.ofSeconds(60), “/usr/bin/sudo”, “/app/scripts/openhab/myscript.sh”)

Just tried this:
executeCommandLine(Duration.ofSeconds(60), “sudo@/app/scripts/openhab/myscript.sh”)
… Same error

that worked:
executeCommandLine(Duration.ofSeconds(60), “/usr/bin/sudo”, “/app/scripts/openhab/myscript.sh”)

Thanks!

1 Like

You’re welcome.
I’m rather new to openhab myself, so I’m not quite sure about the @-variant. It’s possible you’d have to double it up (@@), but I skipped that alltogether, because I didn’t like the look of it.
Glad it works for you now.

I also tried @@, this also doesn’t work…

The @@ stuff is no longer used for OH3 executeCommandLine. Some more info:

Hi,
I tried this:
executeCommandLine(Duration.ofSeconds(60), “sudo@@/app/scripts/openhab/myscript.sh”)

This doesn’t worked…

This.

See Actions | openHAB

Besides the other advices check and make sure that for sudo command openhab is in group sudo. If it is not in it needs to be added to that group.

1 Like

One Further question…

I need to start a script with executeCommandLine. The script needs 2 Parameter with spaces.
How can I start the script out of a rule?
Here’s the working example of OH 2.5:
executeCommandLine("sudo /app/scripts/openhab/mailsenden.sh abc.def@provider.com “ALARM: Rauchmelder ausgelöst” “Rauchmelder im Wohnzimmer hat ausgeloest” ", 60000)

How can I archive this in OH3?
I had tried:
executeCommandLine(Duration.ofSeconds(60), “/usr/bin/sudo”, "/app/scripts/openhab/mailsenden.sh abc.def@provider.com “ALARM: Rauchmelder ausgelöst” “Rauchmelder im Wohnzimmer hat ausgeloest” "

No error in LOG, but Skript isn’t executed…

a) use a comma between the separated arguments

executeCommandLine(Duration.ofSeconds(60), “/usr/bin/sudo”, "/app/scripts/openhab/mailsenden.sh", "abc.def@provider.com","ALARM: Rauchmelder ausgelöst", "Rauchmelder im Wohnzimmer hat ausgeloest")

b) I do not know if quoting for space separated parameters work. So just in case it still does not work concatenate the single words with e.g. minus ( - ) to make them one long string. I case this is not the problem you can remove the minus symbol later again.

c) to use sudo openhab needs to be member of the sudo group which is not the default.
Question: do you really need sudo prvileges for myscript.sh ? For security reasons you should only use sudo where it is required as script will be executed as user root. If sudo is required of course depends on the content of myscript.sh which is not shown

1 Like

All,

I have a similar problem and usually successfully “translated” to OH 3 like:

executeCommandLine(Duration.ofSeconds(9), "sudo", "/bin/chown", "-R", "openhab:openhabian", "/var/www/upload/squirrel/")

However, I truggle with this execution to get the local temperature at our Car:

var String request = "curl" + " -s" + " http://api.openweathermap.org/data/2.5/weather?lat=" + Car_Loc_Lat.state.toString + "&lon=" + Car_Loc_Lon.state.toString + "&APPID=" + W_OWM_Key.state.toString + ""
logInfo("+++ CAR", "Car: Car's Temp request String (request) is: " + request)
var String json = executeCommandLine(Duration.ofSeconds(10), request)

The output string of “request” looks ok and returns the right result in a browser.
However, I get:

Failed to execute commandLine '[curl -s http://api.openweathermap.org/data/2.5/weather?lat=xxx&lon=xxx&APPID=yyy]'

Any suggestion would be greatly appreciated.

I’m not an expert, but I think the issue is that you are passing it as one string. Where there is whitespace separators in the string, all of the components either side of the whitespace have to be passed as separate parameters for it to work.

You are building and passing a single long string containing the whitespace separators, which I don’t think will work.

I think you would need something like -

var String json = executeCommandLine(Duration.ofSeconds(10), "curl", "-s", "http://api.openweathermap.org/data/2.5/weather?lat=" + Car_Loc_Lat.state.toString + "&lon=" + Car_Loc_Lon.state.toString + "&APPID=" + W_OWM_Key.state.toString)

This is the approach you took in your first example that worked.

Thank you Trevor.

In comparison to my capabilities almost everybody here is an expert :wink:

So, I will try your suggestion.
However, doesn’t this create a continuous string as well:

Anyway - I will try and post the results here.

I feel exactly the same way :slightly_smiling_face:

Based on the example you gave, there is no space in that part of the string, so I don’t think it needs breaking into separate parameters. If I’m correct, you only need to break where there are spaces (whitespace) in the command line.

Now I got what you mean - and I see the difference.
You are absolutely right!
It works flawlessly.
Thank you very much!

I’m having also some issues with my executeCommands. :sob:

executeCommandLine("/bin/sh -c (sed -i '1s;^;" + currentTime + " Groot waterverlies? " + waterVerschil + " l \\n " + ";' /var/log/openhab/belangrijk.log)")

But before attacking the sed part, I wanted to have a small working script. What I did as test, was the following. Not sure why it won’t work :blush:

rule "Test 1"
when
        Item TestKnop1 changed
then
        logInfo("TEST", "...Test start...")
        executeCommandLine(Duration.ofSeconds(1), "/usr/bin/sudo", "/bin/echo", "TEST", ">>", "/tmp/test")
        logInfo("TEST", "...Test end ...")
end

I tried already with other files, without full path or sudo…

I also can’t get that to work and I don’t get any error messages in the logs.

A little bit of searching suggests that the exec binding doesn’t work well (or possibly at all) with either redirects or pipes.

See -
https://community.openhab.org/t/rule-issue/106732/9
https://community.openhab.org/t/send-the-value-from-a-slider-to-a-script-py/103782/29
https://community.openhab.org/t/concatenate-inside-exec-command/105983/21

I’d suggest getting a short script to work that doesn’t use a redirect or pipe as a starting point.

Did you try it also without the timeout part?
The timeout should be used when you need the response from your execution.
Therefore the reponse has to be written into a variable.
Maybe thats already the reason for failing here.

If you just want to execute a script and it doesn’t matter what the reponse is, you should omit the duration parameter.

OK, new test. The file is been created. But the text isn’t added.
So it looks liks >, >> or | is giving some issues…

rule "1. Test"
when
        Item TestKnop1 changed
then
        logInfo("TEST", "...Test start...")
        executeCommandLine("touch", "/tmp/test")
        executeCommandLine("echo", "TEST", ">", "/tmp/test")
        logInfo("TEST", "...Test end ...")
end

File that’s been created:

-rw-r--r-- 1 openhab openhab 0 Jan  8 11:12 /tmp/test

I’ve also tried to changed that file rights, but no luck, nothing is been added…