How to solve Exec binding problems

Ah, OK, thanks for clearing that up.

I’ve tried the following:

sudo bash -c echo 50 > /sys/class/backlight/rpi_backlight/brightness
21:04:55.653 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from UNINITIALIZED to INITIALIZING
21:04:55.665 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from INITIALIZING to ONLINE
21:05:00.263 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Switch_Bright_50' received command ON
21:05:00.289 [INFO ] [arthome.event.ItemStatePredictedEvent] - Switch_Bright_50 predicted to become ON
21:05:00.312 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from OFF to ON
21:05:00.327 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from NULL to ON
21:05:00.413 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from ON to OFF
21:05:00.426 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from ON to OFF
21:05:00.434 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_exit changed from NULL to 0
21:05:00.440 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_output changed from NULL to 
21:05:00.447 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_lastexecution changed from NULL to 2019-05-22T21:05:00.414+0100
sudo bash -c 'echo 50 > /sys/class/backlight/rpi_backlight/brightness'
21:06:07.092 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from UNINITIALIZED to INITIALIZING
21:06:07.122 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from INITIALIZING to ONLINE
21:06:13.452 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Switch_Bright_50' received command ON
21:06:13.471 [INFO ] [arthome.event.ItemStatePredictedEvent] - Switch_Bright_50 predicted to become ON
21:06:13.490 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from OFF to ON
21:06:13.502 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from NULL to ON
21:06:13.595 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from ON to OFF
21:06:13.621 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from ON to OFF
21:06:13.638 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_exit changed from NULL to 1
21:06:13.650 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_output changed from NULL to 50: -c: line 0: unexpected EOF while looking for matching `''
50: -c: line 1: syntax error: unexpected end of file
50: -c: line 0: unexpected EOF while looking for matching `''
50: -c: line 1: syntax error: unexpected end of file
21:06:13.663 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_lastexecution changed from NULL to 2019-05-22T21:06:13.578+0100
sudo bash -c "echo 50 > /sys/class/backlight/rpi_backlight/brightness"
21:53:47.914 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from UNINITIALIZED to INITIALIZING
21:53:47.952 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from INITIALIZING to ONLINE
21:53:55.764 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Switch_Bright_50' received command ON
21:53:55.783 [INFO ] [arthome.event.ItemStatePredictedEvent] - Switch_Bright_50 predicted to become ON
21:53:55.809 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from OFF to ON
21:53:55.833 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from NULL to ON
21:53:55.924 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from ON to OFF
21:53:56.008 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from ON to OFF
21:53:56.033 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_exit changed from NULL to 1
21:53:56.050 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_output changed from NULL to 50: -c: line 0: unexpected EOF while looking for matching `"'
50: -c: line 1: syntax error: unexpected end of file
50: -c: line 0: unexpected EOF while looking for matching `"'
50: -c: line 1: syntax error: unexpected end of file
21:53:56.066 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_lastexecution changed from NULL to 2019-05-22T21:53:55.914+0100
sudo bash -c “echo 50 > /sys/class/backlight/rpi_backlight/brightness”
21:10:21.378 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from UNINITIALIZED to INITIALIZING
21:10:21.397 [INFO ] [ome.event.ThingStatusInfoChangedEvent] - 'exec:command:brightness50' changed from INITIALIZING to ONLINE
21:10:29.162 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Switch_Bright_50' received command ON
21:10:29.184 [INFO ] [arthome.event.ItemStatePredictedEvent] - Switch_Bright_50 predicted to become ON
21:10:29.262 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from OFF to ON
21:10:29.280 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from NULL to ON
21:10:29.296 [INFO ] [smarthome.event.ItemStateChangedEvent] - Switch_Bright_50 changed from ON to OFF
21:10:29.308 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_run changed from ON to OFF
21:10:29.319 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_exit changed from NULL to 127
21:10:29.331 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_output changed from NULL to 50: “echo: command not found
50: “echo: command not found
21:10:29.342 [INFO ] [smarthome.event.ItemStateChangedEvent] - exec_command_brightness50_lastexecution changed from NULL to 2019-05-22T21:10:29.230+0100

Still no joy :cry:

I don’t use the Exec binding so I’m probably not going to be of much additional help. Often problems like this can be addressed by putting the commands in a shell script. You can also try to escape the quotes (i.e. \")+.

1 Like

You need to replace the spaces with @@, it’s starting a new command before ‘echo’ because of the space

I did find a couple of bugs with the exec binding (don’t have them to hand) but for a simple command like that should be ok.

Failing that you could use a rule and executecommandline which doesn’t suffer from some of the bugs (see my rule file under the topic ‘lightwave binding updated’ for lots of examples). It also shows you the @@ syntax.

Did you file issues on the binding?

Thanks both,

Tried escape no luck

sudo bash -c /"echo10 > /sys/class/backlight/rpi_backlight/brightness"

Not entirely sure where to put @@ but the following did not work:

sudo bash -c "echo@@10 > /sys/class/backlight/rpi_backlight/brightness"

sudo bash -c@@"echo10 > /sys/class/backlight/rpi_backlight/brightness"

sudo bash -c "echo@@10@@>@@/sys/class/backlight/rpi_backlight/brightness"

sudo bash -c "/bin/echo@@'10 > /sys/class/backlight/rpi_backlight/brightness'"

I’ll try a shell script tomorrow (I think I know how to do that)

The @@ should replace all spaces.

sudo@@bash@@-c@@...

Tried both with no luck:

sudo@@bash@@-c@@"echo10 > /sys/class/backlight/rpi_backlight/brightness"
sudo@@bash@@-c@@"echo10@@>@@/sys/class/backlight/rpi_backlight/brightness"

I then created a shell script and added it in the PaperUI and it worked:

# Create file
sudo touch brightness50.sh

# Make executable
sudo chmod u+x brightness50.sh

# Open
sudo vi /home/pi/brightness50.sh

# Paste
sudo bash -c "echo 50 > /sys/class/backlight/rpi_backlight/brightness"

# Convert
sudo vim +'set ff=unix | x' /home/pi/brightness50.sh

# Run
sudo sh /home/pi/brightness50.sh

Many thanks for your help and suggestions @rlkoshak , much appreciated!

Without testing in a rules file should work:

executeCommandLine("sudo@@bash@@-c@@echo \"10\" >> /sys/class/backlight/rpi_backlight/brightness",5000)

Just looked at your escapes above also, slashes the wrong way :+1:

1 Like

It did! :beers:

.items

Switch ExecSwitch { channel="exec:command:boo:run" }

.rule

rule "Send command via rule"
when
    Item ExecSwitch received command ON
then
    executeCommandLine("sudo@@bash@@-c@@echo \"10\" >> /sys/class/backlight/rpi_backlight/brightness",5000)
end
2 Likes

I haven’t dabbled too much in it but from what I understand you can use the execute command line (exec v1) and remove the exec v2 thing and also the channel from your switch item (you’ll just end up with errors otherwise). Or you can try and integrate the syntax I showed you into the v2 thing and remove the execute command line rule

You should also be able to have the 10 or any other value as an input parameter in the exec v2 binding

@rlkoshak
I have a command I want to run from exec:

sudo /usr/bin/xvfb-run -a --server-args="-screen 0, 1920x1080x24" cutycapt --min-width=400 --min-height=200 --smooth --url="http://localhost:8080/chart?groups=gInTemp&period=D" --out=/etc/openhab2/html/intemp.png

Running from ssh works, even running as user openhab. However when I want to run it from exec binding, the following error appears:

/usr/bin/xvfb-run: 183: /usr/bin/xvfb-run: 0,: not found

I have added this line to the sudoers, so it can execute this without password:

openhab ALL=NOPASSWD:/bin/systemctl restart nodered, /usr/bin/xvfb-run, /usr/share/man/man1/xvfb-run.1.gz

Do you have any idea why this is not working?

When you execute a command from the command line, you are running in a shell and get to take advantage of all the features and capabilities built into that shell. You don’t have a full shell when you run from openHAB.

Often putting the command inside a shell script can address this.

Also make sure the Exec binding is configured correctly. You don’t present the full log line so I can’t tell what the error is coming from. It appears like it can’t find the xvfb-run command.

Thanks. So should I create a shell script which runs this command? I can’t really send you more logs, because there are no errors. This is what I just posted is the Output channel…

Well that is a pretty key piece of information. Then the error is coming from the command, not from the Exec binding.

I would try putting it in a shell script. If that doesn’t work all I can offer is to keep trying stuff. The Exec binding is one of the more challenging ones to get working correctly. You can replace the spaces with @@ for example. The double quotes might need to be escaped. There might be something else going on which is making it so that the openhab user cannot see the path to the command (e.g. running OH in Docker).

Hello

i am not that good at programming and i am trying to achieve a button/switch that restarts my mac. Did someone already do this?

Thank you!

Start here Jonas

thanks a lot! i will have a deeper look into this tomorrow! i tried several times before until i gave up. maybe it can be done this time :wink:

Hi! hm. unfortunately that does not work on a mac. the first command (adduser) is not supported by the mac bash-terminal. i’ll try to goolge my way to success once more :wink:

@rlkoshak So I have just revisited this as I wanted to migrate my exec v1 to exec v2 and I’ve found what the initial problem was:

When using the exec v2 thing if you have a header which has a space in then it doesn’t work.
For example:
-H Authorisation:Bearer Token

Now if I execute the same command in ssh and wrap it in apostrophes as so:
-H ‘Authorisation:Bearer Token’
Then it works

However this doesn’t work in the binding.
I’ve tried so many variations of wraps in quotes/apostrophes/escaped quotes/escaped apostrophes and also replacing the space with @@ but nothing works.

I don’t know how to file the issue or even find out why but with so many apis requiring authorisation like this now it’s stopping the exec binding from being utilised.

If anyone has come across this before and has a way of getting round it then it would be most appreciated.

There usual way to get around that is to write a shell script and call the shell script from the Exec binding.