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.
good hint:
Failed to restart openhab2.service: Interactive authentication required.
See system logs and ‘systemctl status openhab2.service’ for details.
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:
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.
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.