Search:  
Gentoo Wiki

HOWTO_Automatically_turn_off_your_monitor


 XOrg IndexConfigurationHIDevices
FontsVideo CardsMonitors  


Contents

Introduction

I got a new LCD monitor for my Linux system, however I was concerned I would burn out the backlight because Linux would only blank the screen when the computer was not in use, rather than turn off the monitor completely like Windows did. After some searching, I found out how to make Linux do this very easily.

Installation

Install ACPI

see ACPI/Configuration

Configure XOrg to use DPMS.

Edit /etc/X11/xorg.conf. Add the line Option "DPMS" to the Monitor section of the file. Then add the line Option "OffTime" "20" to the ServerLayout section of the file.

File: /etc/X11/xorg.conf
Section "Monitor"
    Option "DPMS"

Section "ServerLayout"
    Option "OffTime" "20"

The number 20 is the number of minutes of inactivity before the monitor is turned off. It will be turned on when you use the mouse or keyboard again. Optionally, you can use StandbyTime for minimal power saving, or SuspendTime for moderate power saving. I use SuspendTime with my LCD, because they all turn off the backlight, but the other two cause my monitor to say "Lost Signal" for a few seconds first.

Nvidia drivers

If you use nvidia-drivers you need to add
Option "DPMS" "TRUE"
to the "Device" Section.
File: /etc/X11/xorg.conf
Section "Device"
    Identifier  "NVIDIA card"
    Driver      "nvidia"
    Option      "DPMS" "TRUE"
EndSection

Note:I think this is not needed anymore. (as of 2008-06-16, with nvidia-drivers-169.12)

Testing

To test if everything is working without restarting X and waiting 20 minutes, you can use the following command from an xterm. standby or suspend may be used in place of off in that command:

# emerge xset -va
# /usr/bin/xset dpms force off
Note: If your monitor turns off and back on immediately when running this command, then there may be a conflict with XScreenSaver. Try running again the command after having killed XScreenSaver. If it works, just remove the OffTime option line from xorg.conf, go to XScreenSaver configuration, set power management from there, restart the XScreenSaver's daemon and finally restart X. Should work like a charm.

Automatically turning the monitor off when closing the lid

If you have a laptop and find that since switching to Gentoo (or another Linux flavour) that the monitor no longer switches off automatically when you close the lid, it's quite simple to get this functionality back:

FIXME: Unlike what was mentioned above, you will need the acpid package on either kernel as this program is used to detect acpi events.

you will need to emerge the laptop-mode-tools package to get the actions file:

# emerge laptop-mode-tools

Edit the acpid script

Using your favorite text editor, open /etc/acpi/actions/lm_lid.sh, it should look something like:

File: /etc/acpi/actions/lm_lid.sh
#!/bin/bash

test -x /usr/sbin/laptop_mode || exit 0

# lid button pressed/released event handler
/usr/sbin/laptop_mode auto

Edit the file, so that it looks like this:

File: /etc/acpi/actions/lm_lid.sh
#!/bin/bash
# lid button pressed/released event handler

#laptop mode helps minimized hdd activity
test -x /usr/sbin/laptop_mode && /usr/sbin/laptop_mode auto

#get  the -xauth variable so we can access the display
XAUTH="$( ps -C X f | sed -n 's/.*-auth \(.*\)/\1/p' )"
if [[ -z $XAUTH ]]
then
# if XAUTH is blank try another way to get it 
      XAUTH="$( ps -C xinit f | sed -n 's/.*-auth \(.*\)serverauth.*/\1Xauthority/p' )"
fi
  
#Find out if the lid is open or closed
if grep -q open /proc/acpi/button/lid/LID/state
then
       ACTION="on"
else
       ACTION="off"
fi

# turn the display off or back on
XAUTHORITY=$XAUTH /usr/bin/xset -display :0.0 dpms force $ACTION

To briefly explain, as this script is called when both a "lid closed" and "lid opened" event is raised, we must find out which state the monitor is now in (hence the need for the grep statement) and act appropriately.

lm_lid.sh edited by daddio to (hopefully) work both for everyone including kdm/gdm users and startxers. tested with kdm and startx. This was based in part on (and should render unnecessary) some of the workaround scripts at the bottom of the page.

Alternatively, if you have a radeon graphics card you can simply emerge --update acpid radeontool and edit the /etc/acpi/actions/lm_lid.sh script like this (and nothing else needs to be configured):

File: /etc/acpi/actions/lm_lid.sh
#!/bin/bash

# lid button pressed/released event handler
test -x /usr/sbin/laptop_mode && /usr/sbin/laptop_mode auto

if grep -q open /proc/acpi/button/lid/LID/state
then
        /usr/sbin/radeontool light on
else
        /usr/sbin/radeontool light off
fi
Note: Users of gnome-screensaver should note that "Lock screen when screensaver is active" will wake the screen up again after closing the lid. Unchecking this option will keep the backlight off.

Now make the command xhost +local:root run whenever X starts, however you wish. This is extremely important.

Edit the .bashrc

Unfortunately, it isn't that simple. Since acpid was most likely started using rc-update, it means it was started as root with no access to your users X display. To me the solution feels like a hack, hopefully someone else can update this section with a better method (I have also seen someone mention security concerns, but he did not elaborate).

Add the following line to your ~/.bashrc file:

File: ~/.bashrc
xhost +local:root > /dev/null

You can also type:

$ xhost +local:root > /dev/null

into a console now (as your normal user, not as root) to have it work immediately.

Otherwise, the next time you log in, your monitor should automatically switch off when you close the lid.

Use the XDM's Authorization File

An alternative to the .bashrc method is to directly use the xdm's authorization file. This trick allows xset to be run even without a user logged in.

In order to do this, you need to first find out the root xauth file used by your xserver. The easiest way to find that out is a ps command (if you are running Xgl, substitute "grep X" with "grep Xgl"):

# ps aux | grep X
root      4287  4.6  4.6  47728 42264 tty7     SLs+ 11:56   7:54 /usr/bin/X :0 -audit 0 -auth /var/gdm/:0.Xauth -nolisten tcp vt7

Take note of the file name following "-auth" in the process. That's the file name (/var/gdm/:0.Xauth) that you need to set to XAUTHORITY variable before you can run xset from the command line. Also take note of the display number of the screen. You will need to specify that in lm_lid.sh also.

Edit your /etc/acpi/actions/lm_lid.sh file:

File: /etc/acpi/actions/lm_lid.sh
#!/bin/bash

if grep -q open /proc/acpi/button/lid/LID/state
then
       XAUTHORITY=/var/gdm/\:0.Xauth /usr/bin/xset -display :0.0 dpms force on
else
       XAUTHORITY=/var/gdm/\:0.Xauth /usr/bin/xset -display :0.0 dpms force off
fi

(If "/etc/X11/xdm/authdir/authfiles/*:0*" in the lm_lid.sh script above doesn't work then try to use "/var/run/xauth/A:0*" instead).

Neither of those options worked for me, so instead I just set it to the .Xauthority file in my user's home directory (IE XAUTHORITY="/home/<user>/.Xauthority" Note: quotations this time, not backticks.). This doesn't work without that user logged in though. --genn

The following script works for me on Gentoo:

XAUTHORITY=/var/run/xauth/`ls /var/run/xauth/`
export XAUTHORITY

/usr/bin/xset -display :0.0 dpms force off

-- pilla

This script also works for me, whereas the "XDM's Authorization File" script did not. I suggest making this a separate section in the contents (sorry, new to wiki's). --Cyrxi 00:01, 30 May 2007 (UTC)


The above script does not work for me, possibly because I start from command line, using startx. As a result, I wrote an (ugly) script that finds the Xauthority through xinit's command line. Here it is:
if grep -q open /proc/acpi/button/lid/LID/state
then
        XAUTHORITY="$( ps -C xinit f|sed 's/.*-auth \(.*\)serverauth.*/\1Xauthority/p' )" /usr/bin/xset -display :0.0 dpms force on
else
        XAUTHORITY="$( ps -C xinit f| sed -n 's/.*-auth \(.*\)serverauth.*/\1Xauthority/p' )" /usr/bin/xset -display :0.0 dpms force off
fi
If you have a comment about it, please PM me, AM088, at the forum. feb 14, 2008, script cleanup, daddio

Locking X11 session when the lid is closed & using xscreensaver throttling when the lid is closed or the power is off

/etc/acpid/lid.sh

export XAUTHORITY="$( ps -C xinit | tail -n 1 | sed -r 's/^.* -auth ([^ ]) .*$/\1/g' )"
export DISPLAY=":0.0"

powerstate=$(gawk '{print $2}' /proc/acpi/ac_adapter/AC/state);
lidstate=$(gawk '{print $2}' /proc/acpi/button/lid/LID/state);
case "$lidstate" in
        open)
                /usr/bin/xset dpms force on
                if [[ -n "$XAUTHORITY" ]]
                then
                    if [[ "$powerstate" == "on-line" ]]
                    then
                        logger -t acpid "unthrottling xscreensaver"
                        /usr/bin/xscreensaver-command -unthrottle &> /dev/null
                    fi
                fi
                ;;
        closed)
                /usr/bin/xset dpms force off
                if [[ -n "$XAUTHORITY" ]]
                then
                    if [[ "$powerstate" == "on-line" ]]
                    then
                        logger -t acpid "throttling xscreensaver"
                        /usr/bin/xscreensaver-command -throttle &> /dev/null
                    fi
                    /usr/bin/xscreensaver-command -lock &> /dev/null
                fi
                ;;
esac

/etc/acpid/ac_adapter.sh

export XAUTHORITY="$( ps -C xinit f | tail -n 1 | sed -r 's/^.* -auth ([^ ]+) .*$/\1/g' )"
export DISPLAY=":0.0"

powerstate=$(gawk '{print $2}' /proc/acpi/ac_adapter/AC/state);
lidstate=$(gawk '{print $2}' /proc/acpi/button/lid/LID/state);
case "$powerstate" in
        on-line)
                if [[ -n "$XAUTHORITY" ]]
                then
                    if [[ "$lidstate" == "open" ]]
                    then
                        logger -t acpid "unthrottling xscreensaver"
                        /usr/bin/xscreensaver-command -unthrottle &> /dev/null
                    fi
                fi
                ;;
        off-line)
                if [[ -n "$XAUTHORITY" ]]
                then
                    if [[ "$lidstate" == "open" ]]
                    then
                        logger -t acpid "throttling xscreensaver"
                        /usr/bin/xscreensaver-command -throttle &> /dev/null
                    fi
                fi
                ;;
esac

Thanks to AM088 for the previous tip, it provided much inspiration.

For KDE

First, install kscreensaver:

# emerge kscreensaver

Then add another line after "blank.sh":

/usr/kde/3.5/bin/dcop --all-users --all-sessions kdesktop KScreensaverIface lock

For GNOME

First, install gnome-screensaver:

# emerge gnome-screensaver

Then add another line after the "dpms force on":

gnome-screensaver-command --lock

For other Window Managers

You have to install Xscreensaver:

# emerge xscreensaver

Then add another line after "blank.sh":

xscreensaver-command -lock

Stubborn LCDs

I got a NEC Versa Litepad tablet PC using the trident driver (modular X) that refuse to turn off LCD backlight - the ACPI works because when connected to external monitor (on dual display "mirorr" mode) the monitor-powerdown happens on the external VGA monitor but not LCD (back-light won't switch off). The backlight stays on even in console (text) mode. I noticed that when X is configured to display on external CRT only, the LCD's backlight do turn off. A possible work around is to use only external display, or restart X to use external display when I want the backlight to be off. Not pretty really, a tablet is not meant to be a stationary server hooked to a monitor.


For stubborn LCD's, you might try vbetool dpms off.


Here Are 2 scripts to use vbetool in KDE or Gnome

Vbedpms-Kde.pl

Something Custom i created to use vbetool with KDE.

#!/usr/bin/perl
# Author: Kevin Blair
use strict;
use warnings;

#time out inseconds after screen saver is up that the monitor goes to sleep
my $suspend=900; 
my $cmd = 'dcop kdesktop KScreensaverIface isBlanked';
my $vbestat=1;

while (my $status = `$cmd` ) {
  if ($status =~ /true/ && $vbestat) {
    sleep $suspend;
    $status = `$cmd`;
    if ($status =~ /true/) {
      system('sudo vbetool dpms off');
      $?||$vbestat=0;
     }
  } elsif ($status =~ /false/ && ! $vbestat) {
    system('sudo vbetool dpms on');
    $?||$vbestat=1;
  }
  sleep 2;
}

Vbedpms-Gnome.pl

Something I found while trying to use vbetool in KDE

#!/usr/bin/perl
use strict;
use warnings;

my $cmd = q{dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver',member='SessionIdleChanged'"};

open (IN, '|-', $cmd);
while (<IN>) {
  if (/^\s+boolean true/) {
    system('sudo vbetool dpms off');
  } elsif (/^\s+boolean false/) {
    system('sudo vbetool dpms on');
  }
}

Links

Gentoo Forums:

Others:

Retrieved from "http://www.gentoo-wiki.info/HOWTO_Automatically_turn_off_your_monitor"

Last modified: Sun, 07 Sep 2008 06:11:00 +0000 Hits: 66,606