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:
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
That looks right.
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
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:~$
I’m trying to replicate the @@ space replacement in executeCommandLine, but can’t get it working.
curl command works directly from openHab commandline, so user rights should be sufficient.
Running the rule doesn’t do what it should supposed to.
rules file:
rule "Write NordPool energy prices to Influxdb"
when
Item Renew_price changed
then
//executeCommandLine("curl@@-i@@-XPOST@@'http://192.168.50.30:8086/write?db=openhab_db'@@--data-binary@@'NP_Electricity_price_test@@value=999@@1435362189575692185'")
executeCommandLine("curl@@-i@@-XPOST@@'http://192.168.50.30:8086/write?db=openhab_db'@@--data-binary@@'NP_Electricity_price_test@@value=999@@1435362189575692185'")
//logInfo("Write NordPool energy prices to Influxdb", results)
end
log:
2019-03-23 21:47:41.662 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '[curl, -i, -XPOST, 'http://192.168.50.30:8086/write?db=openhab_db', --data-binary, 'NP_Electricity_price_test, value=999, 1435362189575692185']'
what is the secret of passing the command with executeCommandLine?
Add a time out argument to the call to executeCommandLine in milliseconds and log out the result of the call. If curl is returning an error it is almost certainly printing an ear message and that will let you see the message.
i did change the rules to the following:
rule "Write NordPool energy prices to Influxdb"
when
Item Renew_price changed
then
//executeCommandLine("curl -i -XPOST 'http://192.168.50.30:8086/write?db=openhab_db' --data-binary 'NP_Electricity_price_test value=999 1435362189575692185'",5000)
executeCommandLine("curl@@-i@@-XPOST@@'http://192.168.50.30:8086/write?db=openhab_db'@@--data-binary@@'NP_Electricity_price_test@@value=999@@1435362189575692185'@@",1000)
logInfo("Write NordPool energy prices to Influxdb","results")
end
log:
2019-03-24 11:02:36.577 [vent.ItemStateChangedEvent] - Renew_price changed from ON to OFF
==> /var/log/openhab2/openhab.log <==
2019-03-24 11:02:36.707 [INFO ] [e NordPool energy prices to Influxdb] - results
command did not execute, as previously.