How to turn on/off Raspberry Pi display? (permission errors)

Hey there,

as written in https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=47550 i used »hdmi_blanking=1« in my /boot/config.txt to put my display connected to the Pi to standby after Pi booted up.
After »apt-get upgrade« and updating from openHAB 2.2 to 2.3 this doesn’t work anymore.

  • Platform information:
    • Hardware: Raspberry Pi 3 Model B
    • OS: openHABian
    • openHAB version: 2.3.0

So I thought: why not switch the display on/off through openHAB and be even more flexible. I have two scripts:
rpi-display-off.sh

#!/bin/bash
tvservice -o

rpi-display-on.sh

#!/bin/bash
tvservice -p
fbset -depth 8
fbset -depth 16
chvt 7
sleep .1
chvt 1

They work quite well on console using sudo - but they don’t work with exec binding.

So I followed OpenHAB sudo [Exec Binding] to give openhab user sudo rights:

$ sudo -u openhab sudo -l
Matching Defaults entries for openhab on openHABianPi:
env_reset, mail_badpass, secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

User openhab may run the following commands on openHABianPi:
(ALL) NOPASSWD: ALL

(I know “ALL” is not good for productive use, one should restrict to necessary commands)

But even with that, the scripts don’t work:

$ sudo -u openhab ./rpi-display-off.sh
[E] Failed to initialize VCHI (ret=-1)

$ sudo -u openhab ./rpi-display-on.sh
[E] Failed to initialize VCHI (ret=-1)
open /dev/fb0: Permission denied
open /dev/fb0: Permission denied
chvt: ioctl VT_ACTIVATE: Operation not permitted
chvt: ioctl VT_ACTIVATE: Operation not permitted

So how do I turn my display on/off using openHAB?

Thanks in advance!

Check the file with> $ls -al /dev/fb0
Is your user added to this group and does the file have the correct permissions?

Oh thanks, that looks good!

$ ls -al /dev/fb0
crw-rw---- 1 root video 29, 0 Oct 3 12:10 /dev/fb0

So I added openhab to video group:

$ sudo usermod -a -G video openhab

Now turning display on and off works (even without openhab in sudoers), but rpi-display-on.sh yields one last error:

$ sudo -u openhab ./rpi-display-on.sh
Powering on HDMI with preferred settings
chvt: ioctl VT_ACTIVATE: Operation not permitted
chvt: ioctl VT_ACTIVATE: Operation not permitted

The chvt lines are needed in case of a double execute of the on script (otherwise screen is on but blank). What’s wrong here?

Oh still can’t call the scripts with openHAB:

2018-10-03 16:03:55.156 [INFO ] [arthome.model.script.raspberry.rules] - Turning Raspberry display on
2018-10-03 16:03:55.320 [INFO ] [arthome.model.script.raspberry.rules] - [E] Failed to initialize VCHI (ret=-1)
open /dev/fb0: Permission denied
open /dev/fb0: Permission denied
chvt: ioctl VT_ACTIVATE: Operation not permitted
chvt: ioctl VT_ACTIVATE: Operation not permitted

What’s going wrong here? I can use sudo -u openhab to run the scripts successfully, but opneHAB itself gets permission denied? weird.

From the terminal what’s the output of $groups

Also check ls -al /etc/openhab2/scripts and make sure the scripts have correct permissions.

Those are correct, as I set them previously. Scripts get executed - otherwise I wouldn’t see the error messages!

$ sudo -u openhab groups
openhab tty dialout audio video bluetooth gpio
$ ls -al |grep rpi
-rwxrwxr-x 1 openhab openhabian 25 Jul 12 22:51 rpi-display-off.sh
-rwxrwxr-x 1 openhab openhabian 80 Jul 12 23:16 rpi-display-on.sh

Only difference is that sudo -u openhab can execute the script properly, but openhab can’t do that - why?

From where I’m sitting, can’t say for sure but shouldn’t openhabian be in sudo -u openhab groups.

Solved it… as unspectacular as it could be: Restart openHAB service and now it runs (with known chvt error message). Seems as group memberships aren’t automatically aplied to running processes

So if someone has a hint for chvt error message, I would be keen to try it.

Have you also used sudo systemctl daemon-reload since making your changes?

I use:

vcgencmd display_power 1

to turn video display on and:

vcgencmd display_power 0

to turn if off. No sudo (root) required.

Hi.
I’ve tried your solution for the same problem, if i try:
sudo -u openhab /script/monitorup.sh
after added openhab to video group and rebooted raspberry, and the result is:

Powering on HDMI with preferred settings
chvt: ioctl VT_ACTIVATE: Operation not permitted

Screen power up but Blank. how have you soved it?