Exec binding not working for me

Hi ,

I have configured like this . but exec binding is not working on my pi

Switch openhab_restart “Restart Openhab” {channel=“exec:command:openhab_restart:run”}
Switch openhab_start “Start Openhab” {channel=“exec:command:openhab_start:run”}
Switch openhab_stop “Stop Openhab” {channel=“exec:command:openhab_stop:run”}

Switch mosquitto_restart “Restart mosquitto” {channel=“exec:command:mosquitto_restart:run”}
Switch mosquitto_start “Start mosquitto” {channel=“exec:command:mosquitto_start:run”}
Switch mosquitto_stop “Stop mosquitto” {channel=“exec:command:mosquitto_stop:run”}

Switch nodered_restart “Restart node-red” {channel=“exec:command:nodered_restart:run”}
Switch nodered_start “Start node-red” {channel=“exec:command:nodered_start:run”}
Switch nodered_stop “Stop node-red” {channel=“exec:command:nodered_stop:run”}

Switch influxdb_restart “Restart influxdb” {channel=“exec:command:influxdb_restart:run”}
Switch influxdb_start “Start influxdb” {channel=“exec:command:influxdb_start:run”}
Switch influxdb_stop “Stop influxdb” {channel=“exec:command:influxdb_stop:run”}

Switch cache_openhab “cache_openhab” {channel=“exec:command:cache_openhab:run”}
Switch tmp_openhab “tmp_openhab” {channel=“exec:command:tmp_openhab:run”}
Switch openhab_backup “openhab_backup” {channel=“exec:command:openhab_backup:run”}
Switch pirestart “Pi Restart” {channel=“exec:command:pirestart:run”}
Switch pishutdown “Pi Shutdown” {channel=“exec:command:pishutdown:run”}
Switch pimount “Pi mount” {channel=“exec:command:pimount:run”}

Thing exec:command:openhab_start [command=“/bin/systemctl start openhab2.service”, interval=0, autorun=true]
Thing exec:command:openhab_restart [command=“/bin/systemctl restart openhab2.service”, interval=0, autorun=true]
Thing exec:command:openhab_stop [command=“/bin/systemctl stop openhab2.service”, interval=0, autorun=true]

//Thing exec:command:cache_openhab [command=“sudo rm -rf /var/lib/openhab2/cache/”, interval=0, autorun=true]
//Thing exec:command:tmp_openhab [command=“sudo rm -rf /var/lib/openhab2/tmp/
”, interval=0, autorun=true]
Thing exec:command:openhab_backup [command=“OHBackup.sh”, interval=0, autorun=true]

Thing exec:command:pirestart [command=“sudo shutdown -r now”, interval=0, autorun=true]
Thing exec:command:pishutdown [command=“sudo shutdown -h now”, interval=0, autorun=true]
Thing exec:command:pimount [command=“sudo mount -a”, interval=0, autorun=true]

Thing exec:command:mosquitto_restart [command=“sudo /bin/systemctl restart mosquitto.service”, interval=0, autorun=true]
Thing exec:command:mosquitto_start [command=“sudo /bin/systemctl start mosquitto.service”, interval=0, autorun=true]
Thing exec:command:mosquitto_stop [command=“sudo /bin/systemctl stop mosquitto.service”, interval=0, autorun=true]

Thing exec:command:nodered_restart [command=“sudo /bin/systemctl restart node-red.service”, interval=0, autorun=true]
Thing exec:command:nodered_start [command=“sudo /bin/systemctl start node-red.service”, interval=0, autorun=true]
Thing exec:command:nodered_stop [command=“sudo /bin/systemctl stop node-red.service”, interval=0, autorun=true]

Thing exec:command:influxdb_restart [command=“sudo /bin/systemctl restart influxdb.service”, interval=0, autorun=true]
Thing exec:command:influxdb_start [command=“sudo /bin/systemctl start influxdb.service”, interval=0, autorun=true]
Thing exec:command:influxdb_stop [command=“sudo /bin/systemctl stop influxdb.service”, interval=0, autorun=true]

Have you tested all of your commands via: sudo -u openhab “your command”

This will verify the command is working and no issue with access.

I would like to point out that I see you stopping openhab and then doing an action via openhab. I"m not sure that will work.

Also, please use code fences so its easier to read.

1 Like

Have you seen the other posts about trying to perform system commands?

The theme of the replies is to do with user permissions and executing commands in environments that can’t be closed.

It’s really worth searching this forum

1 Like

Hi
yes -u openhab have rights . but still not working .

Sujit, you got a lot of stuff going through exec. Can you enable debug logs for this binding and show us whats going on?

As I mentioned above, you are stopping openhab with the exec binding.

Now what would you like from openhab, thats not running, to do next? It’s kinda like turning off my computer and trying to do a web search.

To achieve most of what you have in the things file it may be best to use a script, linked to another script that exist outside of openhab’s running services.

1 Like

Well, to be honest, every user is allowed to do a

systemctl status openhab2.service

but you will need to have permission to do a

systemctl restart openhab2.service

You therefor have to add the user openhab to sudoers with option NOPASSWD.

Please be aware that openHAB won’t restart, as a systemctl restart openhab2.service will stop openHAB, the process will die, so there will never be a restart.


A less complex way to get this working:

Thing:

Thing exec:command:mosquitto [command=“sudo /bin/systemctl %2$s mosquitto.service”, autorun=true]

Item:

String execMosquitto "Mosquitto" { channel="exec:command:mosquitto:input" }

Sitemap:

Switch item=execMosquitto mappings=[stop="STOP",start="START",restart="Restart" ]

As autorun is set to true, the command will be executed whenever the input channel is changed. As the Item is linked with the input channel, the command will be executed whenever the item changes its state. You will get three buttons for one Item

Hi Last few days I am trying the same but no luck . Not able to find out where i am going wrong .
I referred this guide too Exec binding with sudo

my user entry are like this . I tried with ‘date’ in exce but it is executing multiple instance with - Interval = 0 ;Autorun =true.
Mosquitto restart is working with Sudo but not with pi or openhab user


Currently this thing is defined
Thing exec:command:email [command=“sudo /bin/echo sujit”,inetrval=0, autorun=true]

Switch execemail “email” { channel=“exec:command:email:run” }

Switch item=execemail mappings=[ON=“eho”]

on click eho only once system keeps on executing it again and again

2018-08-23 23:43:05.431 [thome.event.ExtensionEvent] - Extension ‘binding-exec’ has been installed.

2018-08-23 23:43:05.620 [hingStatusInfoChangedEvent] - ‘exec:command:email’ changed from UNINITIALIZED to INITIALIZING

2018-08-23 23:43:05.653 [hingStatusInfoChangedEvent] - ‘exec:command:email’ changed from INITIALIZING to ONLINE

2018-08-23 23:43:05.727 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: ‘sujit’

2018-08-23 23:43:05.768 [vent.ItemStateChangedEvent] - execemail changed from ON to OFF

2018-08-23 23:43:05.779 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is ‘sujit’

2018-08-23 23:43:49.293 [ome.event.ItemCommandEvent] - Item ‘execemail’ received command ON

2018-08-23 23:43:49.318 [vent.ItemStateChangedEvent] - execemail changed from OFF to ON

2018-08-23 23:43:49.485 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: ‘sujit’

2018-08-23 23:43:49.567 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is ‘sujit’

2018-08-23 23:43:49.567 [vent.ItemStateChangedEvent] - execemail changed from ON to OFF

2018-08-23 23:44:05.808 [vent.ItemStateChangedEvent] - execemail changed from OFF to ON

2018-08-23 23:44:05.855 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: ‘sujit’

2018-08-23 23:44:05.906 [vent.ItemStateChangedEvent] - execemail changed from ON to OFF

2018-08-23 23:44:05.899 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is ‘sujit’

2018-08-23 23:45:05.935 [vent.ItemStateChangedEvent] - execemail changed from OFF to ON

2018-08-23 23:45:05.995 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: ‘sujit’

2018-08-23 23:45:06.036 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is ‘sujit’

2018-08-23 23:45:06.042 [vent.ItemStateChangedEvent] - execemail changed from ON to OFF

2018-08-23 23:46:06.057 [vent.ItemStateChangedEvent] - execemail changed from OFF to ON

2018-08-23 23:46:06.113 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Exec [OUTPUT]: ‘sujit’

2018-08-23 23:46:06.146 [DEBUG] [hab.binding.exec.handler.ExecHandler] - Transformed response is ‘sujit’

2018-08-23 23:46:06.154 [vent.ItemStateChangedEvent] - execemail changed from ON to OFF

Sujit your screenshots show you logged in as “pi”.
I suggest:
Edit /etc/passwd using sudo vi /etc/passwd

Then locate this line

openhab:x:109:113:openhab2 runtime user,,,:/var/lib/openhab2:/bin/false

Change it to

openhab:x:109:113:openhab2 runtime user,,,:/var/lib/openhab2:/bin/bash

Then do

sudo su --login openhab
Passwd: Give your pi passwd

You will be logged in as openhab.

openhab@openHABianPi:~$

Then try all the sudo commands you are planning to include in exec binding, in above terminal first, and make sure they run fine. The exact errors will be visible there.

Well, you are going the wrong direction… :slight_smile:
You have to grant the user openhab sudo permission. After that the correct command is

/bin/sudo /bin/systemctl restart mosquitto.service

For trying if user openhab has sufficient permission, you don’t need to setup a default bash. just use

sudo su - openhab -s /bin/bash

This will force a login as user openhab with /bin/bash as terminal.
As there is no password set for user openhab, this is the only way to login successfully as user openhab without changing /etc/passwd