How To Turn Off Your Monitor Via Command Line in Ubuntu

As previously written on this blog, I have set up a display in our lobby at work to display the day’s current events and meetings using Ubuntu and a tiny PC. Since this is a display which is on all day, the screensaver and monitor blanking (and other Energy Star features) are all turned off.

Under the auspice of wanting to save energy and also extending the life of a new monitor, someone suggested that we turn off the monitor at night using an electrical timer. A lightbulb went off in my head, that there must be a better way to do this via command line and then run it in the cron.

It turns out the solution is very simple. The xset command is the X server preferences command. It has a simple command to turn off the monitor:

$ xset dpms force off

and to turn the monitor back on:

$ xset dpms force on

You can also check the status of the X server settings by using:

$ xset -q

Also, when dpms turns off the monitor, it will turn back on after a keypress or by moving the mouse. Since this is a lobby display, there is no keyboard or mouse installed in the system.

I’ve rolled this into a little bash script with on, off, and status switches:

#!/bin/bash
export DISPLAY=:0.0

if [ $# -eq 0 ]; then
  echo usage: $(basename $0) "on|off|status"
  exit 1
fi

if [ $1 = "off" ]; then
  echo -en "Turning monitor off..."
  xset dpms force off
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "on" ]; then
  echo -en "Turning monitor on..."
  xset dpms force on
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "status" ]; then
  xset -q|sed -ne 's/^[ ]*Monitor is //p'
else
  echo usage: $(basename $0) "on|off|status"
fi

You can then use cron to turn off the monitor at night, and back on in the morning:

0 20 0 0 0 /home/lobby/monitorControl.sh off
0 7 0 0 0 /home/lobby/monitorControl.sh on

This script will turn it off at 8pm and back on at 7am.

Note that this was written for an Ubuntu system, but the xset command is pretty generic so any system that runs Xserver like RedHat, CentOS, Debian, Fedora, etc should be able to use the script as well.

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Digg
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • StumbleUpon

1 Trackbacks

You can leave a trackback using this URL: http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/trackback/

  1. By Dave Drager on April 14, 2009 at 2:58 pm

    New blog post: How To Turn Off Your Monitor Via Command Line in Ubuntu http://tinyurl.com/caom4t

3 Comments

  1. Tomas M

    I have 2,sometimes 3 computers in my bedroom,they make use of the “blank screen” in the screensaver settings.But the backlight is on despite the screensaver settings and produce a spooky glow when the time comes to meet the sandman.I tested your solution “xset dpms force off” and even the backlight disappeared,tonight and in the future i am going to sleep better…Thank you for a real lifesaver :)

    Posted April 15, 2009 at 5:43 am | Permalink
  2. Daniel B

    In ubuntu Karmic I also had to set the XAUTHORITY var as well as the DISPLAY before it could “open” the display though ssh

    Posted November 19, 2009 at 10:59 am | Permalink
  3. @Daniel B: Thank you for this information, very helpful.

    Posted November 19, 2009 at 11:06 am | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*