Openhab2 executeCommandLine issue

Dear folks.

I’m looking for guidance in trying to resolve an issue I have trying to get an executeCommandLine function to work.
I’d like to use this to run a CLI command to perform a backup.

I installed openhab2 via openhabian on Raspberry Pi3.

What I’m trying to achieve is to run a scheduled backup of the system and dump it on a remote mapped drive.
I tried to get amanda to work but failing in that I saw that the “openhab-cli backup” function is working just great.
That is, when I run it from CLI…so…I’m halfway there…just need to automate this.

My thinking in achieving this is to run a rule…si I created the following:

//Rule to perform automated backup

rule "Make system backup"
    Time cron "0 00 2 ? * * *" // runs everday at 02:00
    val results = executeCommandLine("sudo -u openhab sudo openhab-cli backup",60000)
    logInfo("ExecTest", results)
    sendMail("", "Openhabian : Backup Performed", "")
    logInfo("OpenHabian system", "Backup performed!")

However…this doesn’t work or do anything…expect I do get the info logged and also get the email.
I eventually got a step closer after I realized I need a BINDING…so I installed the EXEC BINDING.
This of course after reading a lot on the community boards.

I also delved into [rlkoshak] Richard’s How to solve Exec binding problems “How to solve EXEC binding problem” for guidance.
Some of the pointers I understand, but others not.

An example of another issue I found…is the “openhab” user which was not in the SUDO list…this I also added.
However, running above script…leaves me with the following error:

2019-06-04 21:54:00.188 [INFO ] [ipse.smarthome.model.script.ExecTest] - openhab is not in the sudoers file.  This incident will be reported.

2019-06-04 21:54:02.042 [INFO ] [thome.model.script.OpenHabian system] - Backup performed!

How do I further investigate to try resolve this?
Any suggestions most welcome.

Should it not just be


Don’t know all the parameters so not sure what -u does, also not sure why sudo is in there twice. I’m not that advanced with Linux/unix but exec defo needs @@ for spaces, and any non command parameters such as adding text to a file needs to be enclosed in \”my text\”
For example if you want to specify the backup file location after backup this would need to be within the escaped quote marks as above

Hi Dave.

Thanks for your input.
So, the following is what I read on the forums…when running a script from executeCommandLine…they are run by default as “ROOT” if I remember correctly.
However, with installation of openhab2, all components and files and permissions is created under user “openhab”. So running the required command/script you need to run it as “openhab” user.
The “sudo -u openhab” ensures that your run your script command as the “openhab” user.

Regaring the suggested “@@”. I’ve read up on this…and it seems that sometimes you may need to use the “@@” instead of “spaces” between words or parameters.
Not quite sure when/where needed…
But, I also tried this…the results being exactly the same as per log provided.
So indeed…the spaces may be an issue…but at the moment there is a more serious problem which is as per the log output that “openhab” user is not in sudoers file…although I did perform the command with no error when I added “openhab” user to the sudo group.

Yeah I have added openhab (in my instance openhabian) to sudo group, can’t remember what post I followed though there were so many.

In your command every single space needs @@, it’s only when there’s some form of input as I described above that it needs to be in escaped quotes.

Ok…I’ve done the following:

I ran “sudo visudo” and added the following:

Now running the script delivers the following error:

2019-06-05 00:10:00.220 [INFO ] [ipse.smarthome.model.script.ExecTest] - Sorry, user openhab is not allowed to execute '/usr/bin/sudo openhab-cli backup' as openhab on openHABianPi.

2019-06-05 00:10:02.140 [INFO ] [thome.model.script.OpenHabian system] - Backup performed!

Not sure if I’m closer to resolving or broken it more.

myself niether but executeCommandLine can be tricky
I recently got a script to run (simple echo command) and almost wet myself… so it does work

I only scanned on the upper part of the thread but the current error you have is because you configure the openhab user to only have permission on /bin/ip and /bin/bash (which is a really bad idea as that basically gives the openHAB user full root permissions). You need to give it permission on the openhab-cli command as well.

But, looking up at your original command, it makes no sense. You are sudo’ing to the openhab user but the command is already running as the openhab user, then you are sudo’ing to root to run openhab-cli. Just use sudo openhab-cli backup.

You will often see commands like you are using when someone wants to test how a command will work as run as the openhab user from the command line where you are running as the user openhabian or pi or some other user. You should never use sudo -u openhab on executeCommandLine and the Exec binding. At best it’s redundant.

But at a higher level, I’m a very big proponent of using the right tool for the right job. openHAB is the right tool for home automation. It’s not the right tool for computer system administration. Backup and restore is a system administration task.

And there is such an easy an built in way to do this using the tools built into Linux to do stuff like this for you. For example, create the following script:

openhab-cli backup

give it execute permissions and drop it into /etc/cron.daily. The cron tool will execute this command once a day automatically for you.

Or you can set it up in the crontab with:

sudo crontab

And enter

0 2 * * * openhab-cli backup

You may need to put the full path to openhab-cli. This will run the command at 2 AM every day.

Dear Rich.

Thank you for above.
I tried first as per your suggestion to use ‘sudo openhab-cli backup’ in the execute line.
But I get the following error in log:

2019-06-05 17:54:43.700 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'auto-backup.rules'

2019-06-05 17:55:00.177 [INFO ] [ipse.smarthome.model.script.ExecTest] - 

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

And so in further reading I ended up with the ‘sudo -u openhab’ as an option.

Ok…regarding your 2nd recommendation…I tried this first…
This actual was my natural thought as the openhab-cli backup is working 100% from CLI.
But I couldn’t get it to work hence I looked at trying this function as a rule.
Another benefit from trying to run it as a rule…since I could monitor easier the result and could send myself an email on completion of such task.

Back to using the CRON method…
What I tried is this :
I added a file to /etc/cron.d and called it ‘backup’
Inside I added ‘0 2 * * * openhab-cli backup’
But this didn’t worked and hence my alternative option.

ok…as I’m typing this…I removed the file from “cron.d” folder.
I did the following:
‘sudo crontab -e’ and added a line similar to “0 2 * * * openhab-cli backup”
I of course just updated the relevant time.

Just ran it and VOILA!!! working…
don’t know why it didn’t want to work before…

So…Rich…thank you for above.

Now that I do have the automated backup resolved…this via cronjob.
How could I setup the system to send me an email on successful job?
I could do this from openhab…but now need to devise a plan to do it from script…perhaps insert a few lines into the ‘openhab-cli backup’ script.

Why not just add a line to the end of your script running the the backup command?

If you wanted to be super slick, you could probably run some commands prior with some sleeps and return the entire file size to ensure your backups are the same size; and perhaps even delete the oldest one after 10 or so backups have ran…

ok…I managed…
I installed msmtp package…I was busy with installation when you sent your comments above.

ok…just did my final testing.
I installed the msmtp package.
Tested from CLI and working fine.
I then manipulated the ‘backup’ script file and added an additional line that sends an email after successful backup was created.

That message in the log is a one time thing. The first time you execute a sudo command as a user you have to acknowledge this warning message. So yes, you need to run sudo -u openhab, but you need to run it from the command line so you can acknowledge the warning. After you acknolwdge it, the message it never appear again.

If you can’t get it to work from the CLI you will have no chance of ever getting it to work from a Rule.

If you have your email configured on the machine, you will get an email with the output of the cron job by default. I recommend ssmtp. It’s light weight and you can configure it to publish to gmail (you may need to configure an application password in the security settings if you have 2 factor enabled on google. There are tons of tutorials for this. I configure it on all of my machines and get the output from all of my cron jobs every morning. I’m not sure if there is special settings in cron necessary to make this happen, it just worked by default on my machines once I installed and configured ssmtp.

Hi Rich.

Thank you for the comments.
As mentioned, I got it working and also the email thing working.

I used msmtp…it was one of the first options I saw when I made a google search.

Feel free to post what you modified so anyone else that follows this thread can also benefit for email notifications when the backup finishes. :wink:

Yes please if you don’t mind sharing your script for others to learn from the example

1 Like

Hi For sure…

ok folks…see here.

As per above…I opted to use ‘msmtp’ mailing program.

the crontab calls the ‘openhab-cli backup’ 1x time every week for me:

I then edited the actual ‘backup’ script file. Location : /usr/share/openhab2/runtime/bin
See here:


getFullPath() {
  specDir="$(dirname "$1")"
  if cd "$specDir" 2>/dev/null; then
    OutputFile="$(pwd)/$(basename "$1")"

Above I’m showing the contents at the top of the file…ignore this.
I’m only showing you that if you open this for the first time to guide to ensure correct file.
Now you should scroll down right to the bottom:

echo "Removing temporary files..."

rm -rf $TempDir

echo "Success! Backup made in $OutputFile"

echo "Subject: Openhab2 successfully completed backup!" | msmtp

echo ""

This last portion is where the backup script ends when backup has been successfull
Can you see the 2nd last line?

1 Like