I’m a software developer who runs Scribophile, an online writing group for serious writers, Writerfolio, an online writing portfolio service for freelancers, and Standard Ebooks, an open source project that produces liberated ebooks for the true book lover.

Turn your laptop screen off with a keyboard shortcut in Ubuntu Karmic

Back in the pre-Karmic days (or, the good ole’ days, as I call them) you could turn your laptop screen off by issuing this simple command:

xset dpms force off

I had a nice script set up and mapped to a keyboard shortcut to turn my screen off whenever I wanted (say if I was using my laptop to play music and wanted to save some battery life). In Karmic, however, a regression causes this command to turn your screen off only briefly. After a few seconds, your screen will turn back on, on its own. This might be an Intel-specific problem—I have the misfortune of having an Intel video card on my Ubuntu install—but I’m not sure.

To get around this problem, I created a slightly different (and slightly less ideal) script that turns the screen off when a shortcut key is pressed, and then turns it back on after they same key is pressed again. Here’s how to get it for your own laptop:

  1. Create a new file using Nano. I put it in a directory called .scripts, you can put it wherever you like:

    nano ~/.scripts/screen-off
  2. Paste the following into your new file using ctrl + shift + v:

    if [ -f $screenOffLockFile ];
            rm $screenOffLockFile
    	notify-send "Screen on." -i /usr/share/icons/gnome/48x48/devices/display.png
            touch $screenOffLockFile
            sleep .5
            while [ -f  $screenOffLockFile ]
                    xset dpms force off
                    sleep 2
            xset dpms force on

    (An explanation follows). Press ctrl + x to quit Nano, then y to save your script.

  3. Make the script executable:

    chmod +x ~/.scripts/screen-off
  4. Open the System menu, go to Preferences, then Keyboard Shortcuts (or press alt + f2) and run gnome-keybinding-properties.

  5. Click Add, enter ‘Turn Off Screen’ under Name and ~/.scripts/screen-off under Command.

  6. After you’ve added the command, it will appear under the Custom Shortcuts header. Click on your new command to assign it a shortcut key of your choice (I use ctrl + alt + s).

And that’s it. Press your shortcut key to turn your screen off, and press the shortcut key again to turn it back on.

What the script does

When you first press the shortcut key with your screen on, the script checks if there’s a certain file in /tmp/. If it doesn’t find it, it’ll create it and then enter an infinite loop that turns your screen off every 2 seconds if the special file still exists (thus working around Karmic’s regression). When you press the shortcut key with your screen already off, it’ll delete the file and do nothing else, causing the first instance of the script to exit its infinite loop (since it no longer sees the special file) and turn your screen back on again.

It’s as simple as that. It’s not as ideal as the pre-Karmic solution, but it’ll have to do until Canonical QA gets their act together.


  1. laurent

    I think the regression comes from gnome-power-management applet

  2. Zibodiz

    Awesome script, assigned it to the ‘sleep’ button on my keyboard — now I don’t even care if that bug gets fixed!

  3. László Monda

    Here’s my workaround: http://monda.hu/blog/2010/01/12/lock-your-laptop-and-turn-off-display-with-the-touch-of-a-keystroke-in-ubuntu-karmic/

  4. Alex Cabal

    Well, it looks like you use the same command that I used to (xset), but you just kill gnome-power-manager to “work around” the Karmic regression. That’s not really an option for me, because gnome-power-manager does a lot of useful things for my laptop and I’d rather keep it running. I suppose you could set up a script to restart it on a keypress or login or something, but that would be functionally very similar to the script in this post.

  5. dr abish adhikari

    Error while trying to run (~/.scripts/screen-off)
    which is linked to the key (s)

    is what i get after doing exactly what you have said. I assigned the Super+s keys.

  6. Alex Cabal

    Have you tried running the script from the terminal? Try opening a terminal and typing:
    cd ~/.scripts/

    If that works, then something funny is going on with the keyboard shortcut. If it doesn’t work, then the error you get will give more info as to what’s really going on.

  7. Raymond

    I had a brute force approach to solve the problem which was to bind a shortcut to:
    sleep 1 && xset dpms force off && sleep 10 && xset dpms force off && ….. 10 times

    I use this to turn off my screen when my laptop is next to my bed at night when I listen to audio books when I can’t sleep. Your solution probably won’t work for me because I’d have difficulty finding the shortcut when half asleep where as just moving the mouse is easier to wake up the screen.

    I generally find that the screen only turns on once or twice and after a minute or so it generally stays off until the screen saver kicks in and keeps the screen off.

  8. e


    gnome-screensaver-command -a && xset dpms force off


  9. Lila

    I got the same error, so I typed what you suggested and the screen switched off, then when I moved the mouse it began going on/off until I ended the process through the System Monitor.
    I didn’t get any other error messages so in this case I have no idea what happened.

  10. Alex Cabal

    That’s the trick with this script—you can’t restore the screen just by moving the mouse, because the script will just shut it off again on its own. To restore your screen, you need to press the key combination you assigned to the script (in my case ctrl+alt+shift+s), or run the script again. Only when you do that will your screen restore itself. It’s not an ideal way of doing things but it’s the only workaround I could think of thanks to the bug in Karmic.

  11. Lila

    Thank you very much for replying.

    Actually, I think it’s a lot better that it doesn’t go away when the mouse moves. The problem is that when I try using the keys assigned (and I have tried several different ones) it keeps giving me:
    Error while trying to run (~/.scripts/screen-off)
    which is linked to the key (6)

  12. Alex Cabal

    Not sure what the problem might be, have you tried running it in the terminal to see what the actual error is that’s happening?
    You can run it by doing:
    $> cd ~/.scripts
    $> ./screen-off

  13. siegie

    Thanks for the tip. 🙂

  14. Michelle

    I got the same error, so I typed what you suggested and the screen switched off, then when I moved the mouse it began going on/off until I ended the process through the System Monitor.
    I didn’t get any other error messages so in this case I have no idea what happened.

  15. Doug Aitken

    Hi Alex,

    Thankyou for this, I wondered why after my Karmic upgrade I couldn’t do my sleep 1 && xset dpms force off or whatever as before.
    I tried the shortcut, for some reason yours gave an error, so did a custom key on my Acer laptop, along with Windows key + s, so I tried Ctrl+` [key to the left of 1 on top row] and it works fine 🙂
    I think the error message is trying to tell your the keyboard shortcut is mapped already.

  16. Ricardo


    The script by itself works pretty well (aka, running it from terminal) but if I keybind it, it won’t work..
    i get this:

    “Error while trying to run (~/.scripts/screen-off) which is linked to the key ()s)
    but like I said, appart from this, it works like a charm

  17. Jay

    Removing the #!/bin/bash made it work for me. Can’t imagine why.

  18. echto

    For those getting

    “Error while trying to run (~/.scripts/screen-off) which is linked to the key”

    You need to use the full path, not the tilde, i.e.

  19. i.am.experimenting

    Alex, i used your script. But the problem i faced was that the monitor was turning blankscreen and then turning off repeatedly one after other, till the script is again called.
    So to kill the gnome-power-manager was the only option for me. So i developed a tiny program that turns the screen off and also restarts the gone-power-manager after any keypress or mouse movement. You can have a look at it at http://code.google.com/p/kanta-turn-screen-off/

  20. 3797

    Use full path insted of ~.

  21. Ubuntu User

    Could use the keyboard shortcut. It said “error while running …”. So made shortcut

  22. Cody

    This script causes my monitor to flash back on every time xset is called.

  23. Rajiv

    Nice code! Its working gr8 on Xubuntu 11.04! i dunno why power management aint working but it doesnt matter….. i assigned the shortcut to the windows logo key and got no problems with it! 😉

  24. Rahul

    It worked.. thanks ! 😀

  25. Henrique Maia

    This was the only solution that truly worked in my case. It still works. And the bug is still around.

    I guess this is one of the things that still fascinates me in Linux. It doesn’t really matter if it’s a bug or a feature, because one can always find a workaround.

    Thanks a lot for this.

  26. David

    Here we are… >8 years later, and this is still relevant!

    Thanks very much for this, very useful! Needed to make a small addition to have it remove the lock file on wakeup from suspend, but otherwise works well.