31

How do you tell if a computer's monitor(s) are turned on/off from the command line in Linux? I've traditionally thought of monitors as output-only devices, but I've noticed the Gnome Monitor Preferences dialog has a "detect monitor" function. Can this be generalized to determine if a monitor is physically turned off?

0x90
  • 39,472
  • 36
  • 165
  • 245
Cerin
  • 60,957
  • 96
  • 316
  • 522
  • 28
    `echo 'Quick! Press a key if you see this :-)'` – paxdiablo Aug 08 '10 at 05:36
  • Although not technically "from the command line", there's a python script [On this SO question](http://stackoverflow.com/questions/10400236/how-to-observe-changes-in-connected-monitors-via-xlib) that does the trick nicely – Barton Feb 01 '14 at 21:44

8 Answers8

15

From systembash.com, here is the code taken from the link, in case it will be down some day:

#!/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
0x90
  • 39,472
  • 36
  • 165
  • 245
  • the question was about determining not doing it – rofrol Nov 06 '19 at 13:55
  • There may be a case where some of your monitors are turned off by power button and some are on. Your answer does not tell which of the monitors are turned on. `xrandr` tells if a monitor is connected and if a display mode has been set up. – jarno Dec 17 '21 at 08:49
  • Even `xrandr` does not tell, if a monitor has been turned off by its power button. I do not know how to determine that via command line. – jarno Dec 17 '21 at 08:53
15

The VESA DDC connection is an I2C connection that can be used to query the presence of the monitor.

Linux exposes the I2C device and userland programs can communicate directly with the monitor with code such as that at http://jaffar.cs.msu.su/oleg/ddcci/

Notice this below: Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]

# ddcci-tool /dev/i2c-2 -e -c -d


ddcci-tool version 0.03

Reading EDID : 0x50@/dev/i2c-2
    Plug and Play ID: SAM00BA
    Input type: Analog

Using ddc/ci : 0x37@/dev/i2c-2

Capabilities:
(type(LCD)vcp(04 05 10 12 60(1 3) B0(1 2) B6 C6 C8 C9 D6(1 4) DC(1 2 3 4) DF))

Controls (valid/current/max):
Control 0x04: +/0/1 [Reset Factory Defaults]
Control 0x05: +/0/1 [SAM: Reset Brightness and Contrast]
Control 0x06: +/0/1 [Reset Factory Geometry]
Control 0x08: +/0/1 [Reset Factory Default Color]
Control 0x0e: +/60/120 [SAM: Image Lock Coarse]
Control 0x10: +/0/100 [Brightness]
Control 0x12: +/50/100 [Contrast]
Control 0x16: +/8/16 [Red Video Gain]
Control 0x18: +/8/16 [Green Video Gain]
Control 0x1a: +/8/16 [Blue Video Gain]
Control 0x1e: +/0/2 [SAM: Auto Size Center]
Control 0x20: +/50/100 [Horizontal Position]
Control 0x30: +/25/54 [Vertical Position]
Control 0x3e: +/39/50 [SAM: Image Lock Fine]
Control 0x60: +/1/3 [Input Source Select]
Control 0x62: +/0/100 [Audio Speaker Volume Adjust]
Control 0x6c: +/140/255 [Red Video Black Level]
Control 0x6e: +/127/255 [Green Video Black Level]
Control 0x70: +/121/255 [Blue Video Black Level]
Control 0xb0: +/0/2 [Settings]
Control 0xb6: +/3/8 [???]
Control 0xc6: +/1/1 [???]
Control 0xc8: +/5/16 [???]
Control 0xc9: +/1/0 [???]
Control 0xca: +/2/2 [On Screen Display]
Control 0xcc: +/2/11 [SAM: On Screen Display Language]
Control 0xd6: +/1/4 [SAM: DPMS control (1 - on/4 - stby)]
Control 0xdc: +/4/4 [SAM: MagicBright (1 - text/2 - internet/3 - entertain/4 - custom)]
Control 0xdf: +/512/0 [VCP Version]
Control 0xe0: +/0/2 [SAM: Color preset (0 - normal/1 - warm/2 - cool)]
Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]
Control 0xe2: +/0/1 [???]
Control 0xed: +/108/255 [SAM: Red Video Black Level]
Control 0xee: +/101/255 [SAM: Green Video Black Level]
Control 0xef: +/103/255 [SAM: Blue Video Black Level]

An interesting question is whether or not your monitor returns that piece of data, and if not, whether it responds at all if it's currently turned off.

Joe Koberg
  • 25,416
  • 6
  • 48
  • 54
  • 3
    It does. It is even possible to turn your monitor on and off from the command line. – Eren Tantekin May 09 '12 at 22:06
  • 2
    ddcci is now ddccontrol: http://sourceforge.net/projects/ddccontrol/ As Eren said, it does, and you can even do it yourself with `ddccontrol -r 0xe1 -w 0` – domen Mar 21 '13 at 09:49
  • For my case, ddccontrol errors out (complains about monitor not being supported) when the monitor is on and connected, but ddccontrol is silent when not connected or is off. Still looking for a method to determine if the monitor(TV) is on another input though. But that is probably beyond what is feasible. Also, to get this running under Ubuntu 12.04 see here: http://www.techytalk.info/ubuntu/ddccontrol/ and you probably need to do a `sudo modprobe i2c-dev` to get it to work – Dave Butler Mar 05 '14 at 20:14
  • If you are willing to spend some time on it, then you can start looking at i2cdetect and i2cdump, and probe everything, all addressed (could be risky?) and then output the results to a text file, then make some change, like input select... then output the results again, and then do a diff, you might be able to discern what the bytes mean... – Dave Butler Mar 05 '14 at 21:18
  • How would you enable required modules, if you have Intel graphics? – jarno Dec 17 '21 at 08:57
  • `ddccontrol` didnt detect my monitor/tv changes but the `udevadm monitor --property` did – alchemy Apr 11 '22 at 02:34
5

You can get some info using the xrandr command-line utility, if your video driver supports this extension.

haggai_e
  • 4,689
  • 1
  • 24
  • 37
4

Not all monitors support vesa DDC. Thing might got even more complicated if you use a dock.

On the other hand, there is a way to check whether your actions are detected by monitoring the kernel/udev events. To do this, for Fedora and RHEL, type following command:

sudo udevadm monitor --property

It will display every kernel and udev events it detected. From that, you can try plug/unplug the monitor data cable; plug/unplug the monitor power cable; toggle the stand-by/on states by pressing the power button.

If no output is generated after an action, then your system cannot detect it.

Ding-Yi Chen
  • 2,830
  • 33
  • 27
3

You might want to look at the output of

$ xset -q

I'm not sure if it will work but I think the line " Monitor is (on|off)" should tell you the answer.

matthewbauer
  • 4,314
  • 1
  • 17
  • 22
  • 3
    Still gives me `Monitor is On` even when I turn it off. – Thomas Aug 08 '10 at 07:15
  • 2
    That might just show if the monitor's plugged in or not. I don't think there's a way to see if it's actually turned on. If it's plugged in, the OS assumes that it's on. Gnome probably just detects the monitor when it's plugged in not when it's on/off. I guess the only way to try this out is to type out the previous command, unplug your monitor, hit enter, plug monitor back in and see what it says. I can't test this b/c I'm on a laptop. – avacariu Aug 08 '10 at 07:47
  • 5
    "Still gives me Monitor is On even when I turn it off" - how can you tell? Maybe it's smart enough to go back and change the output when it detects you've turned your monitor on to check. Now I just have to figure out whether the fridge light _really_ goes off when you shut the door. – paxdiablo Aug 08 '10 at 07:49
  • 2
    With ssh from a different terminal, which has its monitor not turned off? – user unknown Aug 10 '10 at 00:42
  • If you turn your monitor off in the middle of a datacenter and nobody is there to execute "xset -q," does the computer really register the change in state of the monitor? – Hut8 Aug 11 '10 at 19:20
  • 1
    The VESA DDC controller must be getting power even though the display is off. – Joe Koberg Aug 11 '10 at 19:22
  • This works, and should get more upvotes. However it only works for when the system turns on/off the monitor(s) (ie. power settings). Perhaps if the answer mentioned this. Furthermore, it only seems to work when run manually. I just tried using it on a cron job and got `(Invalid MIT-MAGIC-COOKIE-1 keyxset: unable to open display ":0.0")` – Josh C Jan 09 '20 at 06:08
2

When using xset it always returns xset: unable to open display ""

However, "xset dpms force off" & "xset dpms force off" commands actually turn my monitor off and on. I am using the script outlined here -

http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/

1

xset -qis the way to go for a raspberry pi. A check to see if the reply contains 'Monitor is On' is a great way to use a gpio pin to turn off an LCD Backlight;

if(runOSCommand("xset -q").contains("Monitor is On")){
            System.out.println("Monitor is On");
            if screenLight.isHigh()) {
                screenLight.low();
            }
        }else{
            System.out.println("Monitor is Off");
            if (screenLight.isLow()) {
                screenLight.high();
            }
        }


     public static String runOSCommand(String command){
     String s = null;
     String string = "";
     Process p;
     try {
         p = Runtime.getRuntime().exec(command);
         BufferedReader br = new BufferedReader(
             new InputStreamReader(p.getInputStream()));
         while ((s = br.readLine()) != null){
 //            System.out.println("line: " + s);
             string += s;
         }
         p.waitFor();
//         System.out.println ("exit: " + p.exitValue());
         p.destroy();
     } catch (Exception e) {}
     return string;
 }
Palmeta
  • 61
  • 5
1

First, find the name of the display you want to inspect:

xrandr -q

Then change 'THE-MONITOR' to the proper name:

#!/bin/sh    
is_on="`xrandr -q | grep -A 1 'THE-MONITOR' | tail -1 | sed 's/[^\*]//g';`";

Pipeline:

  • xrandr -q lists all monitors;
  • grep -A 1 'THE-MONITOR' filters to two lines, the one containing the name of your display, and the consecutive line, which will have an "*" next to it if the monitor is on;
  • tail -1 discards the first line;
  • sed 's/[^\*]//g' filter out everything but the "*";

Now "$is_on" is a boolean string as "*" or empty.

This will only work if your preferred mode is at the top of the mode list, which is very usual.

The whole script for turning on and off:

#!/bin/bash
is_on="`xrandr | grep -A 1 'DVI-I-1' | tail -1 | sed 's/[^\*]//g';`";
if [ "$is_on" ]
then
    xrandr --output DVI-I-1 --off
else
    xrandr --output DVI-I-1 --auto --left-of HDMI-0 
fi
fde-capu
  • 205
  • 1
  • 4
  • 14