Bash script execution from rule

Hi!
I need to run a bash script from a rule like this:

rule "Cron job every minute"
when 
       Time cron "0 0/1 * * * ?"   // every 1 minutes
then

    createTimer(now, [ |
        var results = executeCommandLine("/etc/openhab2/scripts/checkBTicino.sh", 5000)

        // code to process results   
        logInfo("cronBTicino", "results : ", results)
    ])
end

The checkBTicino.sh script is:

#!/bin/bash

VAR=$(/usr/bin/openhab-cli console -p habopen bundle:list | grep icino | wc -l)

if [[ $VAR -eq 1 ]]

then

echo β€œTUTTO OK ($VAR binding attivo)”

else

echo β€œNot 1 !! ( $VAR bindings )”

fi

It works from console, storing β€œ1” in VAR , but from rule VAR always equals 0.

Thanks for any help !!
Alex

/------------------------------------------------/
* Hardware: RaspBerry Pi 3B+
  * OS: Raspbian
  * Java Runtime Environment: openjdk version "1.8.0_222"
  * openHAB version: 2.5
/------------------------------------------------/
var results = executeCommandLine(Duration.ofSeconds(5), "/etc/openhab2/scripts/checkBTicino.sh") 

Try

VAR=$(echo bundle:list | /usr/bin/openhab-cli console -p habopen | grep icino | wc -l)

instead of

VAR=$(/usr/bin/openhab-cli console -p habopen bundle:list | grep icino | wc -l)

@denominator ( I am sure that you know it … )

is OH3 syntax acording to

OH2 syntax is required and thus the call to

should be fine.

Thanks @Wolfgang_S for your reply.
Unfortunately it doesn’t work:

VAR=$(echo bundle:list | /usr/bin/openhab-cli console -p habopen | grep icino | wc -l)

returns 0 (zero) from console and from script :frowning:

That is strange. In an other case moving the command into echo helped.

So I checked to see if it works from the command line and it does but there is one difference that I was not aware of before. Using this combination of comands it looks like the length of the output in a line is shortened to a specific amount of characters. It could be that the string you are looking for is cut off.

e.g. output in the console

230 β”‚ Active β”‚  80 β”‚ 2.5.12                  β”‚ openHAB Add-ons :: Bundles :: Amazon Echo Control Binding

output via command line

230 β”‚ Active β”‚  80 β”‚ 2.5.12                  β”‚ openHAB Add-ons :: Bundles :: A
1 Like

80 character limit? The spirit of the punched card lives on …

1 Like

Thanks @rossko57 but it’s definetively a filesystem permission problem!!

With

chown openhab:openhab checkBTicino.sh

I get this in openhab.log when script is fired from rule:

2021-06-22 18:22:00.747 [WARN ] [lipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program β€œ/etc/openhab2/scripts/checkBTicino.sh” (in directory β€œ.”): error=13, Permission denied)

It works fine from raspbian prompt with sudo:

sudo ./checkBTicino.sh
[sudo] password for openhabian:
TUTTO OK (1 binding attivo)

Instead with

sudo chown root:root checkBTicino.sh

I have no permission problem but it does not work (VAR =0 always) from rule.
It is still working from linux prompt:

sudo ./checkBTicino.sh
[sudo] password for openhabian:
TUTTO OK (1 binding attivo)

I’m going mad :face_with_symbols_over_mouth: :face_with_symbols_over_mouth: :face_with_symbols_over_mouth:
Thanks for any suggestion!!!
Alex

The openhab service normally runs as user openhab.
You can check that by having a look to which is the user the java process runs under ( ps -ef ).
Thus the user openhab:openhab needs to have read ( r ) and execute ( x ) permissions on the executable. You can check that by using

ls -ld /etc /etc/openhab2 /etc/openhab2/scripts /etc/openhab2/scripts/checkBTcino.sh

to check if settings for all directories and the script at once.
In case a directory is not owned by user openhab nor group openhab world needs to have (r)x permissions.

Running your script with sudo permssion is a hint to that the ownership is done in a way that the owner/group does not have read/execute permission.

Redirect the output of your script to a temporary file ( before greping ).
You may do this to check how the output looks like executing it from a rule. Just to check if there is any output at all or if the lines are shorter than expected and thus there is no match.
( it could be that there are two problems ( permission and length of the output lines ) )