[SOLVED] Openhab 2 and executeCommandLine / Exec

I struggle with executeCommandLine. I’m pretty sure that the rule worked in the past. Can’t say if the upgrade to 2.3 broke it.

rule "HAB_Restart"
when
	Item HAB_Restart changed from OFF  to ON or
	Item HAB_Restart changed from NULL to ON
then
	logInfo("HAB_Restart", "openHAB is restarting!")
        executeCommandLine("systemctl@@restart@@openhab2", 60000)
	sendCommand(HAB_Restart, OFF)
end

I see “openHAB is restarting!” in the log, so the rule gets triggered, the item gets reset, but the command is not executed.

Any idea?

Try to get the response and post it to the log, maybe this will give more insight.

good hint:
Failed to restart openhab2.service: Interactive authentication required.
See system logs and ‘systemctl status openhab2.service’ for details.

Did you get it solved with that? Or is there a question left?

Hi All,

I have a backup script that executes, at the end of the script it prints a success.

Is there a way I can log this success (or fail) in the openhab log?

This is my current rule (backup works, but the logging doesnt)


rule "Backup OH2/Influx/Grafana"
when
 Time cron "0 0 1 ? * * *" or
 Item backupserver received command ON
then
 logInfo("Server Backup", "Backing up the Server")
 var String RCloneOutput = executeCommandLine("sudo /etc/openhab2/scripts/backup.sh", 60000)
 logInfo("Server Backup", "Backup Executed + RCloneOutput")
end

Due to the very long time out
I would put this in a timer:

rule "Backup OH2/Influx/Grafana"
when
    Time cron "0 0 1 ? * * *" or
    Item backupserver received command ON
then
    logInfo("Server Backup", "Backing up the Server")
    createTimer(now, [ |
        var String RCloneOutput = executeCommandLine("sudo /etc/openhab2/scripts/backup.sh", 60000)
        logInfo("Server Backup", "Backup Executed" + RCloneOutput)
    ])
end

That way you don’t block a thread for up to a minute
See:

1 Like

So close I was ha!

I saw this in the log:


16:34:17.522 [INFO ] [.smarthome.model.script.Server Backup] - Backup Executedsudo: no tty present and no askpass program specified

I tried without sudo, it runs but fails because it cant load files etc. So needs to run as sudo

Thanks, doing this fixed it:

As it is mentioned in this post 36 there is no password.

So adding the user openhab to the sudoers with no password should help.

 sudo visudo -f /etc/sudoers.d/010_pi-nopasswd

Adding following lines

openhab ALL=(ALL) NOPASSWD: ALL

First, THANK YOU for using visudo. I’m always amazed at how many people don’t and then argue with me after they trash their system that they like nano better.

Secondly, please don’t give OH permission to run anything with sudo without password. You may as well run OH as root. Replace the last ALL with the full path to the command that you want OH to be able to run without password, which in this case would be the path to your backup.sh script. This will allow the openhab user to only run that one command without a password, all others will require a password.

Hi Rich

Like so?

openhab ALL=(ALL) NOPASSWD: /etc/openhab2/scripts/backup.sh


1 Like

That looks right.

1 Like

Hi Rich

trying to expand on this to allow ‘openhab-cli backup’ be executed by the openhab user, rather than requiring a password for sudo.

I made this change:

openhab ALL=(ALL) NOPASSWD: /etc/openhab2/scripts/backup.sh
openhab ALL=(ALL) NOPASSWD: /usr/bin/openhab-cli

but it appears executing that from the command line still requires a password. Any thoughts?

By default the openhab-cli script is owned by root:root

1 Like

How would i be able to have openhab user execute that?

I think you can list the commands all on one line separated by a comma.

openhab ALL=(ALL) NOPASSWD: /etc/openhab2/scripts/backup.sh, /usr/bin/openhab-cli

If that doesn’t work, you can create an alias. See

Thanks Rich, im not sure its working

kris@openhab2:~$ sudo -u openhab openhab-cli backup

#########################################
openHAB 2.x.x backup script
#########################################

Please run this script as root! (e.g. use sudo)
kris@openhab2:~$

if I run it as root, it works and doesnt ask for a password but I assume its using root:root

You are missing a sudo.

The sudo -u openhab part of the line runs the script as the openhab user. Then you want to run sudo openhab-cli backup to start as the openhab user and test that openhab can call that script with sudo.

sudo -u openhab sudo openhab-cli backup

Goodness!

OK. It appears the visudo modification didnt work. Time to look at an alias - i assume a command alias?

kris@openhab2:~$ sudo -u openhab sudo openhab-cli backup
[sudo] password for kris:
kris@openhab2:~$