How to Determine if LCD Monitor is Turned on From

2020-05-15 15:11发布

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?

7条回答
Juvenile、少年°
2楼-- · 2020-05-15 15:31

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

查看更多
等我变得足够好
3楼-- · 2020-05-15 15:34

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/

查看更多
Melony?
4楼-- · 2020-05-15 15:37

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
查看更多
混吃等死
5楼-- · 2020-05-15 15:42

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.

查看更多
狗以群分
6楼-- · 2020-05-15 15:43

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.

查看更多
Bombasti
7楼-- · 2020-05-15 15:47

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;
 }
查看更多
登录 后发表回答