Lock your laptop and turn off display with the touch of a keystroke in Ubuntu Karmic

2010-01-12 14:33:06 GMT

I think this feature will soon be standard in Ubuntu as many users requested it. It's absolutely mandatory for me because every time I leave my laptop I carry out this action, even at home. Yeah, call me paranoid...

I've written a simple script to deal with the issue:

#!/bin/bash
gnome-screensaver-command -l
sleep 3
xset -display :0.0 dpms force off

You're encouraged to bind it to any key combo. It should work perfectly out of the box but a gnome-power-manager related bug enables the display some seconds or minutes later randomly, so we have to

killall gnome-power-manager

and it should be pretty fine. For those who can't afford to live without gnome-power-manager an alternative (and in my opinion suboptimal) workaround exists.


Comment written by Pablo at 2010-12-15 02:17:57 GMT:

Hi Lazlo! I've been using your script and followed your sugestions, except for removing gnome-power-manager or killing it.

However, I think I've found a (quite simple) workaround for this (so called) "bug" (which now I'm not really sure it is so)... just increase the number of seconds to sleep to, lets say... 5.

So, my script goes like this:

#!/bin/bash
gnome-screensaver-command -l
sleep 5
xset -display :0.0 dpms force off

Now, everything works just fine! :)

For those of you who don't want to lock the screen, I would recommend to sleep 1 sec before executing xset (otherwise it won't work). So, this second script would include the following lines:

#!/bin/bash
sleep 1
xset -display :0.0 dpms force off

Apparently, it might be that clicking or moving the mouse or maybe pressing the key combo, sends later events... that's why, Linux first needs some time to deal with those before you can successfully call xset.

Hope this helps you all in my same situation!

Greetings from Argentina! Pablo.


Comment written by Laci at 2010-12-15 11:51:55 GMT:

Hey Pablo,

I've just tried your method of waiting 5s instead of 3s and it didn't work. You may be using an upgraded version of gnome-power-manager which doesn't have this bug. Anyways, I'll update to 10.10 soon and hopefully this problem will get solved.


Comment written by michael at 2010-12-19 06:11:18 GMT:

For those who use xscreensaver, you can get around locking the screen and disabling gnome-power-manager by just using:

xscreensaver-command -activate
sleep 0
xset dpms force off

the sleep 0 is probably not even necessary.


Comment written by michael at 2010-12-19 06:34:22 GMT:

spoke to soon, that doesn't work either. you still have to disable gnome-power-manager


Comment written by Laci at 2010-12-19 22:13:54 GMT:

Thanks for trying another workaround, Michael. Yeah, this bug is quite nasty and there is no workaround except killing gnome-power-manager.