How to use "executeCommandLine" correctly

I have the following Rule, but Version 1 of this rule always gives me a wrong result:

rule "test execute Command"
when
    Item Dummy_3 changed to ON
then
//  Version 1 - funktioniert nicht - ich kann den Grund nicht finden
    var pingresult = executeCommandLine(Duration.ofSeconds(20),"/bin/ping","-c5","-W1", "192.168.178.75", ">", "/dev/null", "&&", "/bin/echo", 'online', "||", "/bin/echo", 'offline')  // String in einer Variablen zwischenspeichern
    logInfo("execute-Version 1","String - pingresult: {}", pingresult)                             // Log  pingresult
//  Version 2 - Der Shell-Befehl verpackt in einem Bash-Script, dann klappt's
    pingresult = executeCommandLine(Duration.ofSeconds(10),"/bin/bash","/etc/openhab/scripts/ping_test.sh")  // String in einer Variablen zwischenspeichern
    logInfo("execute-Version 2","String - pingresult: {}", pingresult)                             // Log  pingresult
//  Version 3 - Shell-Kommando - einfach
    pingresult = executeCommandLine(Duration.ofSeconds(2),"/bin/ls", "-alF")  // String in einer Variablen zwischenspeichern
    logInfo("execute-Version 3","String - pingresult: {}", pingresult)                             // Log  pingresult

/* Das Bash-Script (ping_test.sh)- als Beispiel - Einfacher geht's mit dem Network-Binding ;)

#!/bin/bash
INPUT=$(/bin/ping -c5 -W1 192.168.178.99 >/dev/null && /bin/echo 'online' || /bin/echo 'offline')
echo $INPUT

*/

end

As a result I get in the log:

2021-06-27 13:30:37.266 [INFO ] [.core.model.script.execute-Version 1] - String - pingresult: ping: offline: Der Name oder der Dienst ist nicht bekannt
2021-06-27 13:30:41.404 [INFO ] [.core.model.script.execute-Version 2] - String - pingresult: online
2021-06-27 13:30:41.434 [INFO ] [.core.model.script.execute-Version 3] - String - pingresult: insgesamt 56
drwxrwxr-x+ 12 openhab openhab  4096 Jun  3 00:05 ./
drwxr-xr-x  31 root    root     4096 Dez 26  2020 ../
drwxrwxr-x+  5 openhab openhab  4096 Jun 25 13:50 cache/
drwx------+  3 openhab openhab  4096 Dez 30 14:05 .config/
drwxrwxr-x+  5 openhab openhab  4096 Jun  2 23:35 config/
drwxrwxr-x+  3 openhab openhab 12288 Jun  2 23:33 etc/
drwxrwxr-x+  3 openhab openhab  4096 Jan 26 00:23 jsondb/
drwxrwxr-x+  2 openhab openhab  4096 Jun  3 00:05 kar/
drwxrwxr-x+  2 openhab openhab  4096 Jun  5 22:40 .karaf/
drwxrwxr-x+  5 openhab openhab  4096 Dez 26  2020 persistence/
drwxrwxr-x+  2 openhab openhab  4096 Dez 26  2020 secrets/
drwxrwxr-x+ 11 openhab openhab  4096 Jun 25 13:53 tmp/

When I use the command in the console everything works fine:

hab3@hostHAB3:~ $ /bin/ping -c5 -W1 192.168.178.99 > /dev/null && /bin/echo 'online' || /bin/echo 'offline'
online
h

Does anyone know what I’m doing wrong in my command:

var pingresult = executeCommandLine(Duration.ofSeconds(20),"/bin/ping","-c5","-W1", "192.168.178.75", ">", "/dev/null", "&&", "/bin/echo", 'online', "||", "/bin/echo", 'offline')

Every help or hint is welcomed and thx in advance.

Cheers,
Peter

You can not use redirection, pipeing with starting multiple programs in executeCommandLine.
You can start only 1 program with its parameters.
Perhaps the function name executeCommandLine is misleading, it should be executeProgram or executeProcess.

If you want redirection and pipeing with starting multiple programs you have to use a script as you tried or use bash with the command line as direct parameter:

var pingresult = executeCommandLine(Duration.ofSeconds(5), "/bin/bash", "-c", "/bin/ping -c5 -W1 192.168.178.99 > /dev/null && /bin/echo 'online' || /bin/echo 'offline'")
3 Likes

thx, that’s it.
Cheers,
Peter

Just one more short question! What does the parameter “-c” in the “bin/bash”-program stands for ?

Edit: Sorry, Option found, when typing in “man bash” :upside_down_face: :upside_down_face: :upside_down_face:

If I type man bash I get this:
-c If the -c option is present,
then commands are read from
the first non-option argu‐
ment command_string. If
there are arguments after
the command_string, the
first argument is assigned
to $0 and any remaining ar‐
guments are assigned to the
positional parameters. The
assignment to $0 sets the
name of the shell, which is
used in warning and error
messages.

1 Like

Hello togehter,

I will use this topic, because I believe this is the one where my issue fits best.

I try to use the executeCommand Line. But it does not work.

I use openhabian 4.0 and want to restart a bundle, when it gets offline.
I already was able to create a SSH Key. So the command “ssh -p 8101 openhab@localhost bundle:restart 264” from openhabian shell works as expected. The Bundle 264 restarts without password for karaf.

Now I want to use a rule and try a lot of commands, like

executeCommandLine(Duration.ofSeconds(10), "/etc/openhab/scripts/opnsense.sh")

But it did not work. Everytime, when I check the Logfile, I got the message

2023-08-11 14:58:18.969 [WARN ] [shd.server.session.ServerSessionImpl] - exceptionCaught(ServerSessionImpl[null@/[0:0:0:0:0:0:0:1]:41726])[state=Opened] IOException: Broken pipe

Do any of you have an idea, why it is not working.
I also tried the exec thing, but same result.

Did you see this thread?
https://community.openhab.org/t/tutorial-restart-binding-from-rule/37047

Hi Oliver,
I already saw this Thread,
but to be honest, it doesnt help me.
As I wrote, it is possible to login into karaf by the command ssh -p 8101 openhab@localhost.
I do not need the password. I also can run the script from user “openhab”. The script is very simple.
Just one line → ssh -p 8101 openhab@localhost bundle:restart 264

This also works. The only thing I do not understand is, why i cannot rund this directly by a rule.
I also tried different users and chmod properties.

openhabian@openhabian:/etc/openhab/scripts $ ll
total 24K
drwxrwxr-x  2 openhab    openhab    4.0K Aug 11 14:06 ./
drwxrwxr-x 15 openhab    openhab    4.0K Oct 19  2022 ../
-rw-rw-r--  1 openhab    openhab     236 Jun 27  2022 readme.txt
-rwxrwxrwx  1 openhab    openhab      49 Aug 11 12:14 restartBundleSomfy.py*
-rwxr-xr-x  1 openhab    openhab      49 Aug 11 12:22 restartBundleSomfy.script*
-rwxrwxrwx  1 openhabian openhabian   49 Aug 11 13:01 restartBundleSomfy.sh*

Whe I run comand ./restartBundleSomfy.script, the OH thing somfy does a restart as expected.

openhabian@openhabian:/etc/openhab/scripts $ ./restartBundleSomfy.script
openhabian@openhabian:/etc/openhab/scripts $

Try this:

#!/bin/sh
echo bundle:restart org.openhab.binding.somfy | /usr/bin/openhab-cli console -p habopen

Assumed the bundle is called org.openhab.binding.somfy

1 Like

Hi Wolfgang,

perfect. It works, whenever I do no understand the reason :slight_smile:

Hi Wolfgang,
I might have a similar problem.
From command line the following command works without any problems:

sudo smartctl -a /dev/sda | grep 'Temperature Sensor 1:'

However, when I execute this command through a bash script in the following way:

case "$1" in
	...)
	SSD_Temperatur)
		cmd="sudo smartctl -a /dev/sda|grep 'Temperature Sensor 1:'"
		msg="SSD_Temperatur";;
esac
echo $msg
$cmd

I get the following error message:

ERROR: smartctl takes ONE device name as the final command-line argument.
You have provided 4 device names:
/dev/sda|grep
'Temperature
Sensor
1:'

How do I need to change the cmd variable?

Change the last line from $cmd to

`$cmd` 

Unfortunately this does not work either. It appears that none of the parameters are accepted. Just smartctl gets executed.

Try this:

sh -c "$cmd"
2 Likes

Thanks. That works! :+1: