Rule with root right

  • Platform information:
    • OS: openhabianpi
    • openHAB version: 3

I created a python script that needs root user rights for detection bluetooth devices advertising. I want to run this script every x minutes. Is it possible to run a rule with elevated rights?

No, it is not possible to run a rule with elevated rights. There are several approaches though, some better than others. From worst to best:

  1. Run OH as root. Seriously, don’t do this.

  2. Add the openhab user to sudoers configured to run that command without a password. Then modify your call to the script by appending sudo to the front of it. This is slightly better but you’ll have to learn parts of Linux you may not already know to set it up properly.

  3. Don’t have OH call this script at all. Instead use the system cron job system call your script once a minute or how ever often you like and modify your script to push the results to OH via some mechanism OH understands: e.g. OH’s REST API, MQTT messages, etc.

Yeah, Option 3 is currently the preferred way. I am publishing the result already via MQTT, but I am failing to add the script to the cron system. I don’t know to add it that it’s run with root rights

Found the solution, I have added the following via

sudo crontab -e
*/1 * * * * python3 /etc/openhab/scripts/chipolo.py