Lock the screen and save the environment
June 20, 2009 | 12:13If you lock the screen with Ctrl+Alt+L (in gnome), a screensaver starts and you’ll be asked a password next time you press a key. That’s a pretty nice thing if you getting a pause from a public PC but from the environment point of view it doesn’t change anything because your machine is going to consume the same power if you are sitting in front of it or not.
You can change the things by using a command to turn your monitor off :
$ sleep 10 && xset dpms force off
then you have 10 seconds for locking the screen in the usual way.
A more friendly way to accomplish the same task is to use a keybinding and a little script:
#!/bin/bash gnome-screensaver-command -l xset dpms force off
Now just run gconf-editor and edit /apps/metacity/keybinding_commands/command_1 and /apps/metacity/global_keybindings/run_command_1 or, if you are using compiz, run ccsm and edit the respective values in General Options. No, you can’t use Ctrl+Alt+L but you may choose Ctrl+Alt+K (which I happily use)
The above script is very simple but it’s far from being perfect. Here is something more complex:
#!/bin/bash # lock the screen and start screensaver gnome-screensaver-command -l # store the string returned by "gnome-screensaver-command -q" # We have to do this because the string is in your language # which is not predictable :S activestring=$(gnome-screensaver-command -q) if [ -z "$activestring" ]; then echo "Error: null active string!" >&2; exit -1; fi sleep 1; # if either the screensaver is active AND the user is not entering the password for unlocking the screen then we # force the screen to remaing turned off. # In addition, if the the paswword form is active the user has 30 seconds to type in the password. After this period the form # is killed and we return to the screensaver + monitor switched off situation while [ "$(gnome-screensaver-command -q)" = "$activestring" ]; do if ps aux | grep -q gnome-screensaver-[d]ialog; then sleep 30 if [ "$(gnome-screensaver-command -q)" = "$activestring" ] && ps aux | grep -q gnome-screensaver-[d]ialog; then killall gnome-screensaver-dialog; else exit; fi fi xset dpms force off sleep 10; done
The above script comes with two major features:
- the monitor is turned off every 10 seconds if the screen is still locked (sometimes it just tune itself on :O )
- to unlock the screen, the user has 30 seconds to insert a valid password. After that, the monitor is turned off again and the password prompt is killed
On my home PC, the above script allows me to save about 30W (~30% of the total power consumption) while I am away. Well, hibernation (suspend to disk) would be a lot better but often you simply can’t do that.









