[SOLVED] How to configure and Item to execute a basic rules and any command line console from UI

Hello community , Can someone give some guidance about to configure and Item and Sitemap to execute any command line console from Ui.

Thanks for your willingness.

How about reading this

Hello Jürgen , thank you so much for the link, it contains valuable information.

Thanks again.

The biggest problem will be the input of commands. BasicUI has no control that takes a string. Look into habpanel for that.

I see in most examples that this work executing a script, it seems don’t work with simple commands like “sudo systemctl restart apache2” or any other service.
Please can someone give me some guidance about???

Thanks for your comments.

Yes, see this.
Write the java script in the html to send the value to a string item that triggers a rule that sends the state of the item as a command to console with the executeCommandLine action!

Hello Let me shows what i have configured till now. But there is a problem, when I execute the script directly into the SSH command line it works fine, but I execute from UI Switching the Item “Restart_openhab” it does not work , in Log appear the next:

2018-12-12 01:28:59.180 [INFO ] [clipse.smarthome.model.script.reboot] - 

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

I’ve a script which contains the next:

#! /bin/bash
clear
sudo systemctl stop openhab2.service

Also in Items I’ve wrote

Switch Restart_openhab "Restart OPenhab" 

In Sitemaps

sitemap home label="Casa de Julian"{
    Frame {
        Switch item=Restart_openhab icon="Switch" 
    }
}

In .rules

rule "Restart Raspberry"
	when
		Item Restart_openhab changed to ON
	then
		 sendCommand(Restart_openhab, OFF)
                Thread::sleep(3000)
val results=executeCommandLine("sudo /etc/openhab2/scripts/Hola.sh", 5000)
logInfo("reboot", results)	
					
end

Also I’ve confgured the file sudoers, here the result,my user is “openhabian”( I’ve installed openhabian into raspberrypi) then I’ve included user “openhabian” into the file sudoers.

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL
openhabian ALL=(ALL) NOPASSWD: ALL
sudo    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

``
Please someone give me some light about fix this problem.

Thanks for all.

Hello could someone help me , Please.

You have to acknowledge the message that sudo displays the very first time you run sudo from the openhab user. You can’t do this from a Rule. You need to do it from the command prompt. There are a couple of ways you can do that. Here is one.

sudo -u openhab sudo ls

Run that command from the command prompt and acknowledge the message that comes up. Then that message in the log will go away.

Now, if you are trying to restart OH from a Rule this is almost certainly not going to work. Once OH is closed down all of it’s subprocesses also get closed down. This means that the call to systemctl gets killed as well and it gets killed before it can bring OH back up.

Finally, you need to give the openhab user permissions on sudo, not openhabian.

Hello rikoshak.

All you said is quite clear but my main user in openHabian is openhabian when I write the command whoami in SSH command line openhbian user is the result.
If I execute this command

all the way the command prompt ask for the password of the user openhab which I dont know.

Thanks for you willingness

The user you log in as is openhabian.

The user OH actually runs as is openhab. Therefore when you run a command from openhab (i.e. Exec binding or executeCommandLine) that command will also be running as the user openhab.

Are you sure it is asking for the openhab user’s password? If so then that means you haven’t added openhab to sudoers.

At firts when Openhabian start the very firts time openhabian is the sole user that the system acknowledge.

Yes, this command ask for the openhab user’s password.

What you mean is I must to create user openhab and add it to sudoers???.

Thanks rikoshak


This link indicate that the default user is openhabian

Thanks for your help

No, a typical Linux system will have dozens of users. Almost every program that runs as a service runs under it’s own user. openHAB is no different in this respect. The openHAB service runs as the openhab user.

If you install openHAB, part of the installation is to create an openHAB user.

It’s a special type of user though. It doesn’t have a password nor does it have a shell. So you can’t log in as this user.

No, the openhab user already exists. Yes, you must add it sudoers.

The default user for logging on to the system is openhabian.

Run cat /etc/passwd. Every single line in that file represents a user on your system. You should find the openHAB user somewhere near the bottom.

openhab:x:110:114:openhab2 runtime user,,,:/var/lib/openhab2:/bin/false

The /bin/false at the end indicates that this user doesn’t have a shell and you cannot log in as it.

Thank very much for your answer, I really apreciate your willingness.

I wiil make some test and then I tell you what was the result.

Thank you very much again.

Hello rikoshak and others members who participated.

Before some test finally I´ve could execute the command thanks for your help.

This information was like gold,thanks all post’s participants.