5

xset dpms force off doesn't work, outputting:

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  147 (DPMS)
  Minor opcode of failed request:  6 (DPMSForceLevel)
  Serial number of failed request:  12
  Current serial number in output stream:  14

I have two monitors that have power buttons on them, with default power management settings. However, pressing one keyboard shortcut using a command would be handy.

I'm pretty sure I'm using Gnome and read that xset doesn't work with Gnome.

As an alternative solution I'll just change the screen timeout to 1 minute.

Related:

James Ray
  • 393

5 Answers5

4

This script works on my Ubuntu 17.10

#!/bin/bash

busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 1
read -n 1 -s -r -p "Press any key continue"
busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 0
Gonki
  • 163
1

I took inspiration from Gonki and James Ray's answers and wrote a (python) version which runs without a shell:

#! /usr/bin/python3
import subprocess

template = ('busctl --user {what}-property org.gnome.Mutter.DisplayConfig '
            '/org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig '
            'PowerSaveMode')

current = subprocess.check_output(template.format(what='get').split()).strip()
try:
    if current == b'i 0':
        subprocess.call(template.format(what='set').split() + ['i', '1'])
    else:
        subprocess.call(template.format(what='set').split() + ['i', '0'])
except:
    # If anything goes wrong, turn on the light:
    subprocess.call(template.format(what='set').split() + ['i', '0'])

It should solve the problem that Johannes Lemonde reported (that the screen would immediately turn on again), and hence will work with any hotkey: just press the same hotkey again to turn the screen on.

1

Ubuntu 17.10 runs a Wayland session by default, click the gear icon at login and choose a Xorg session instead if you need commands like xset. A common rule of thumb is that commands starting with x only run in Xorg. As far as I know Wayland doesn't provide a way to turn off the screen yet. This may very well change in the future though.

dessert
  • 40,956
1

Update 23 Mar 2018: alternatively you can set the power button to suspend rather than turn off. 24 Mar: however if you suspend it then the execution of all processes will be halted, which may not be desirable.

I entered the following commands made by @Gonki into the file turnthescreenoff.sh which I saved in my home directory, then entered bash /home/james/turnthescreenoff.sh as a keyboard shortcut.

#!/bin/bash

busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 1
read -n 1 -s -r -p "Press any key continue"
busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 0
James Ray
  • 393
0

Here's a script in bash similar to Pietro Battiston's answer ( I would rather have added a comment but I can't ) :

#!/bin/bash
p="org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode"

[ $(busctl --user get-property $p | cut -d ' ' -f 2) -eq 1 ] && s=0 || s=1

busctl --user set-property $p i $s

Each call of the script toogle the screen on or off.