executeCommandLine on a .sh script doesn't do anything

Hello,
I’m trying to run an script file from an executeCommandLine in my OpenHAB rules. The log shows that it saw the push button and executed the command, but I don’t think it’s actually running the shell script. Here’s what I have:

rule "frm_off_rule"
        when
                Item itm_frm_off received update
        then
                executeCommandLine("/home/pi/scripts/run_test.sh")
end

The <run_test.sh> looks like this:

pi@raspberrypi:~/scripts $ cat run_test.sh 
mosquitto_pub -h localhost -t 'teststart' -m '1'

I’ve tried running the script file and I do indeed see the MQTT publish, so there’s nothing wrong with the script.

I see the log file trying to execute the script

2018-04-16 11:45:52.451 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine ‘bash /home/pi/scripts/run_test.sh’

I’ve tried adding sudo in a few places - in the script before the mosquitto_sub, and also adding it inside the executeCommandLine itself. I’ve tried adding bash in the command too.

executeCommandLine("sudo /home/pi/scripts/run_test.sh")
executeCommandLine("bash /home/pi/scripts/run_test.sh")

But that doesn’t help. I have another Pi running Raspbian that uses the same setup, and that one works. Looking through the forum posts, this seems to happen, but it’s a mystery why it happens. Any tips?

sudo probably doesn’t work since you have to enter your password there…

is run_test.sh owned by the same user as openhab runs on (probably openhab)…?

How did you run the script? did you use ‘sh scriptname’ or ‘./scriptname’? The reason for asking is that the ‘sh’ method is tolerant of non-unix type files, whereas the direct execution mode isn’t. This usually happens when you’ve edited the script on a windows machine, and copied it to your Linux machine. If this is the case, you can use vim to fix it, with the command :set ff=unix.

I guess, openHAB runs as user openhab, while you are executing your script as pi. The script is also located in pi’s home folder.

Please check:

  • the user which runs openHAB
  • run the script as this user

My guess is that you need to add a shebang to the top of the file:

#! /bin/bash
mosquitto_pub -h localhost -t 'teststart' -m '1'

And make sure that the OpenHAB user has execute-permissions.

sudo chmod +x /home/pi/scripts/run_test.sh

If the purpose of this script is to just publish an MQTT message you should be using the MQTT Action or MQTT Binding, not a shell script.

1 Like