38

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
  • 521

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
  • 28,986
eriv
  • 481
30

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,707
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!

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