37

How can I make a script (or make some changes) to allow me to turn the screen off in Ubuntu (12.04) whenever I want, the way thevscreen can be turned off in Cellphones?

And how can I make a combination of keys (a shortcut) to execute that script?

I want to conserve power.

I've seen this question

Turning Monitor Off With an Icon but there is no hotkey for it.

Moreover, in the answer, it is said that there are many ways to accomplish this so I'd like to know more.

Tarun
  • 511
  • 1
  • 5
  • 14
  • 3
    Possible duplicate of [How to turn off monitor by hotkeys?](http://askubuntu.com/questions/216653/how-to-turn-off-monitor-by-hotkeys) – Cas Jan 26 '17 at 14:42
  • Reversing the direction of close (voting to leave this open) as this is the better post imho – Zanna Jan 26 '17 at 19:12
  • To assign custom key combination use these steps from [Official ubuntu documentation](https://help.ubuntu.com/stable/ubuntu-help/keyboard-shortcuts-set.html) – AndreyCh Nov 15 '15 at 20:59

4 Answers4

34

If I understood well your question, you could use this commmand

xset dpms force off

In your settings, go to keyboard, shortcut, click on plus, write the name you want and past the command I just wrote, click on the new short cut and use the combination or key you want to use

kiri
  • 27,676
  • 16
  • 81
  • 117
eriv
  • 481
  • 5
  • 12
27

Custom shortcuts to do this with newer versions of Ubuntu (e.g. 16.04) seems to require a delay from pressing the key combination to running the command. The simplest solution is to modify the shortcut command using sh.

Go to System Settings... | Keyboard | Shortcuts | Custom Shortcuts. Create a new custom shortcut and in Command put:

sh -c 'sleep 0.3 && xset dpms force off'

You can change the delay but I found 0.1s was not quite enough sometimes.

enter image description here

Cas
  • 8,427
  • 5
  • 69
  • 108
12

The command is xset dpms force off

Just create a keyboard shortcut for it. Go to keyboard->shortcuts....go to custom shortcuts.....& then click on the + icon to create a new shortcut.

Then assign the above & assign custom key combination.

enter image description here

DONE!

Khurshid Alam
  • 3,040
  • 3
  • 25
  • 35
9

At the beginning I also configured a key shortcut with

xset dpms force off

But after 2 seconds the display turned on again. Maybe my newer Ubuntu Version (15.05) is the reason for this different behaviour. The solution was to create a script with the following content:

#!/bin/bash

sleep 1 && xset dpms force off

Now call this script every time you press the shortcut.

I think this method works because of any delayed key press event. With the sleep command all key events have 1 second to be processed and they do not wake the screen up again.

Martin
  • 361
  • 2
  • 4