Execute a Shellscript (as root) when pressing a button in the sitemap

I try to add a Button in my sitemap which run a simple Shell script on local Raspberry when the button is pressed. The script must run as user “root”, so a ‘sudo …’ must be added (or I have to add this part in the shell script).

(The EXEC binding is installed in oH)

I’m not shure, must I create a rule to realize this?

Checkout this topic. It does a restart of openHAB, but you can modify that solution to run your shell script instead.

Looks like you duplicated your question.

Thanks for the link.

The sudoers - part I will check later, actual I’m standing on the line …

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

Where have I to add it? In a rule? Didn’d I need to configure it in an item-file?

I want a button on a sub-screen to execute the shell-action.

Let’s step back. You would be well served to review the concepts section of the Docs because your question belies a lack of understanding of the important concepts that make up openHAB and how they work together.

You need to:

  1. Create an Exec binding Thing
  2. Create one or more Items associated linked to the Channel(s) of the Thing
  3. Add the correct Item to your sitemap.

The sudo part is kind of essential in this solution. Without it, it will not work because openHAB would need to provide a password to execute the script which cannot be done. Hence, you need to configure the OS such that it knows that it does not require a password when the openhab user uses sudo to execute your script.

The Thing goes into a *.things file.

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

The switch Item to trigger the script goes into an *.items file.

Switch openhab_restart "Restart Openhab" {channel="exec:command:openhab_restart:run"}

The button you want to show on your Sitemap should be defined in a *.sitemap file.

sitemap demo label="Your Value"
        Frame {
            Switch item=openhab_restart

Later you may want to provide a button in HABPanel as well, but it is easier to first get it to work using a sitemap.

Be sure to also checkout the docs from the exec-binding. It contains examples (unfortunately not using sudo) and more information about other channels made available by the exec-binding, e.g. how to get the output of the executed script.

1 Like

this is working now,
a “Switch” is executing a script which runs under user “root” !

But my "Switch should not a “switch”, should be more a “Push button” (not be “ON” after “clicking”.
(or going in state “off” after 2 seconds, but how? . New rule?)

At the end I have 4 different scripts which should be started, so I think a bether solution is to use one “button” with 4 options instead for single switches.
In a sitemap I have this for a rollershuter (and lamps), to select a specific value with pressing the wanted selection. See example-picture.lamp_0-3

But this switch should be not in the sitmap, must be in sub-screen.

Actuall I put this single switch (in your example “openhab_restart”) in my “gVswitches” group.
Is it possible to create a switch with 4 selections (and every selection start a different script, maybe with a case-part in a rule (case: 1=start-script1; 2=start-script-2, …))

Also after pressing one from the (4) options, the value must/should not be stored and shown as “active” (pressed) in the display: all 4 options should be going into state “inactive” (Off).

And Rich, you are right, I try do understand more and more in oH … learning with examples like these are very good! My problem was the “Virtual” action, because I had no physical item to send a value, now I see: I can use a channel-binding to execute a shell-script.

I’m currently at work, so I can’t provide you with a full answer.

Fixing the button to stay off should be easy to accomplish adding the autoupdate parameter to your item and setting it to false (although I have never used it with a channel before, only with a binding as show in the documentation).

Switch openhab_restart "Restart Openhab" {channel="exec:command:openhab_restart:run", autoupdate="false"}

Parameter autoupdate=“false” works as you expected, thanks! Switch is going to “off” again after pressing.
(I only had to install the RegEx-Transformation, this is needed for this parameter (?!))

My other question (push-button with 4 selections to start 4 different scripts) is still waiting for help (maybe you have later an ideo), or others …

Create a Number Item or a String Item.

Put that Item on the Sitemap as a Switch with a Mappings. See https://www.openhab.org/docs/configuration/sitemaps.html#mappings

You will then need a Rule to trigger and send the command to the right Switch linked to the script.

In a sitemap, OK, I think I know how I can do this, have already similar “Buttons” there with 3 or 4 selections (rollershutter, lamp).

Is it possible to use a mapping in a items, because the Number is created in cam.items like:

Number cam_select
  "cam to start"

But here it seems a …mapping … is not allowed.

How can I get this 4-selection-button in the group gVswitches to display?

You can only define a mappings in the sitemap.

You can’t use a mappings on the sitemap for an Items put on the sitemap using a Group element.