Softpedia
 

NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
TRENDING TODAY
Home > News > Linux > Ubuntu Tips and Tricks

September 24th, 2011, 13:01 GMT · By

Easily Turn Off LCDs With a Shortcut Key in Ubuntu

SHARE:

Adjust text size:


Enlarge picture
This tutorial will teach Linux users how to make a script in order to easily turn off their netbook or laptop's monitor (LCD screens) with a keystroke, to save battery life.

Are you using Linux and you want to save the life of your laptop/netbook's battery? Look no further, we have the solution for you! The following guide will teach you step-by-step with screenshots, how to make a script and create a shortcut key in order to easily turn off your LCD screen with a keystroke, to save the life of your notebook's battery.

We've tested this tutorial on the newly released Ubuntu 11.10 Beta 2 (Oneiric Ocelot) operating system and it's working like a charm. Please follow the instructions below carefully.

Step 1 - Install the requirements

First of all, we need to install a Python library, so just click the link below and let your package manager install the required package. After installation, close the package manager and proceed to the next step.


Install python-xlib


Step 2 - Make the script

Now that you've installed the required library (see above), all you need to do is to right click on your desktop and select the "Create New Document -> Empty Document" option.

Review image

Rename the file to monitor_off.sh. Now, open it with a double click, copy the code below and paste it in.

#!/usr/bin/python

import time
import subprocess
from Xlib import X
from Xlib.display import Display

display = Display(':0')
root = display.screen().root
root.grab_pointer(True,
        X.ButtonPressMask | X.ButtonReleaseMask | X.PointerMotionMask,
        X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)
root.grab_keyboard(True,
        X.GrabModeAsync, X.GrabModeAsync, X.CurrentTime)

subprocess.call('xset dpms force off'.split())
p = subprocess.Popen('gnome-screensaver-command -i'.split())
time.sleep(1)

while True:
    print display.next_event()
    p.terminate()
    break

Save the file!

Review image

Right click on the monitor_off.sh file and choose "Properties".

Review image

Go to the "Permissions" tab and check the "Allow executing file as program" option.

Review image

Editor's note: Place the monitor_off.sh file somewhere on your home folder, but remember the path for the next step.

Step 3 - Make the shortcut

Click the Ubuntu logo to open the Unity Dash and search "keyboard" (without quotes). Click the "Keyboard" icon and the Keyboard app will open.

Review image

Go to the "Shortcuts" tab, select "Custom Shortcuts" option from the list and click the + button to add a new shortcut. In the "Name:" field write something like "Monitor Off Script" and in the command field write the path to the script, including the name of the script (e.g. /home/softpedia/monitor_off.sh). Click the "Apply" button when you're done.

Review image

To assign a keyboard shortcut, click the "Disabled" entry on the newly created shortcut line and press the desired key on your keyboard, when it says "New shortcut".

Review image

We've assigned F10 as the shortcut key for our monitor off script.

Review image

That's it! Close the Keyboard app and press the selected shortcut key to test the script. Is your monitor off now? Turn it back on with the Esc key or with a mouse click.

If you encounter issues with the tutorial, do not hesitate to drop a comment below. Have fun using Linux!


16,917 hits · 25 comments
Link to this article · Print article · Send to friend

MUST-READ RELATED ARTICLES:


Ubuntu 11.10 Beta 2 Released, Synaptic Removed

Kororaa Linux 15 Released, Based on Fedora 15

Pardus Linux 2011.2 Has Been Released

GeeXboX 2.0 is Based on XBMC Media Center

Softpedia Linux Weekly, Issue 165

READER COMMENTS:


Comment #1 by: myamy on 23 Sep 2011, 19:51 UTC reply to this comment

Or...
Just set the screen-saver to "blank" (why use a screen-saver if your intention is to lengthen usage time anyway), and use the "ctrl-alt-l" hot-key to enable.

Comment #1.1 by: NotMe on 26 Sep 2011, 17:34 GMT

This is kind of strange since 11.10 battery life is 1/3 of my dual boot Win7. Where is the script to fix that?


Comment #2 by: qlue on 24 Sep 2011, 00:27 UTC reply to this comment

don't all laptops and netbooks have a hardware key for exactly this purpose these days?


Comment #3 by: Cl_One on 25 Sep 2011, 06:03 UTC reply to this comment

Tried it and it works on Linux Mint 10 Gnome. Thanks for the tutorial.


Comment #4 by: Ravi on 26 Sep 2011, 10:51 UTC reply to this comment

Thanks Softpedia! Now I can turn off my LCD anytime!!!


Comment #5 by: martin on 26 Sep 2011, 21:21 UTC reply to this comment

what about:
create launcher - application - command:
xset dpms force off

works for me too ;-)

Comment #5.1 by: gonza on 21 Apr 2013, 19:09 GMT

Much more simpler. Thank you!


Comment #6 by: nickname on 27 Sep 2011, 08:00 UTC reply to this comment

Why don't you just assign shortcut:
bash -c "sleep 2 ; gnome-screensaver-command --lock ; xset dpms force off"
to some key? What's the need for python? And dependencies?

Comment #6.1 by: BoZo on 28 Sep 2011, 00:49 GMT

nice I used this bash command as a launcher (terminal based). Put on panel in Ubuntu 10.04 and it works great.


Comment #7 by: Anon on 27 Sep 2011, 09:09 UTC reply to this comment

A python script to perform something that is trivial in bash???:

#!/bin/bash
sleep 2
xset dpms force off

Python is major overkill for this task.

Comment #7.1 by: Charlie on 28 Sep 2011, 03:52 GMT

The Python script works fine. The screen comes back on after about a minute with the bash script


Comment #8 by: grumpy gills on 29 Sep 2011, 18:34 UTC reply to this comment

What's the icon-like picture of at the top of the article? That looks cool.

Comment #8.1 by: Marius Nestor on 30 Sep 2011, 10:26 GMT

It's a Luxeed keyboard... a LED illuminated keyboard for your PC :)

http://www.youtube.com/watch?v=WNdt12mI5dE


Comment #9 by: Ravi on 24 Oct 2011, 07:11 UTC reply to this comment

This doesn't works in Ubuntu 11.10 64 bit. When I press F10 i get this error " Error while trying to run (/home/ravi/Monitor_off.sh) which is linked to the key (F10)." So please update the article because it is outdated and doesn't work!!

Comment #9.1 by: navi on 09 Jan 2012, 06:12 GMT

i too am getting this error. running 10.04

Comment #9.2 by: julio on 14 Jan 2012, 00:25 GMT

I get the same error, I double checked the file and path name, is there a way to debug?

Comment #9.3 by: jay on 19 Apr 2012, 10:45 GMT

I'm using Ubuntu 11.10 and the display comes back immediatelly... doesn't work for me, too :(

Comment #9.4 by: eagle on 26 May 2012, 06:35 GMT

i got error that too and i was saving the script in a folder inside the home folder, I just left the script inside the home folder without being inside another folder and it worked fine, no idea why butit works that way

Comment #9.5 by: anthero on 10 Jun 2012, 08:20 GMT

try removing the " / " before 'home/path_to_file' .. this works on fedora too

Side note:
Is there a way to make it so the screen only comes back to life after hitting the same shortkey again?
I have an old laptop where the screen wont turn off when i close the lid. i would like to remotely access it without the screen coming back on..


Comment #10 by: msx on 12 Nov 2011, 05:25 UTC reply to this comment

*cof*
$ xset dpms force off
*cof cof*


Comment #11 by: hzeta on 10 Jan 2012, 15:22 UTC reply to this comment

It works on Ubuntu 11.10 too. Thanks


Comment #12 by: imdgood1 on 13 Feb 2012, 19:07 UTC reply to this comment

** (gnome-screensaver-command:22734): WARNING **: Unknown option -i
Xlib.protocol.event.MotionNotify(event_y = 370, state = 0, type = 6, child = , detail = 0, window = , same_screen = 1, time = 32134492, root_y = 370, root_x = 162, root = , event_x = 162, sequence_number = 10)


Comment #13 by: Anonymouse on 08 Aug 2012, 14:24 UTC reply to this comment

I earlier saw this exact same script here:

http://ubuntuforums.org/showthread.php?p=9433671#post9433671

Where is the acknowledgment to that earlier (=more original?) post?

Comment #13.1 by: omelette on 16 Sep 2012, 17:53 GMT

This tip does NOT work with the current Ubuntu 10.04 - it works fine when run from Bash manually, but as a shortcut, the screen switches back on. But if people just try what several have suggested, ie. forget about this Python-dependent script and use the much simpler bash-script, the shortcut works perfectly!


Comment #14 by: HappyUser on 18 Dec 2012, 11:26 UTC reply to this comment

Thanks so much! It works perfectly on my Chakra Linux.

Copyright © 2001-2013 Softpedia. Contact/Tip us at

WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM