I use a script in /etc/acpi
to turn off the screen (xset dpms force
) when I close my laptop's lid. Now I want it to act differently
off
when an external monitor is connected (set brightness to zero
instead). I know I can test whether it is connected with xrandr
,
but the script is run by root, and xrandr depends on a certain
X-session. Is there a way for root to test whether the monitor is
connected, regardless of X-sessions? I'm using Arch Linux.
Can root test whether a monitor is connected
monitors
Best Answer
Method #1 - edid-decode
You can use
edid-decode
to see what monitors are connected. This is from an Ubuntu 16.04 system I have, but this worked in Fedora 28 & CentOS 7.x as well.NOTE: This particular system has two video ports, I'm using the VGA one.
Here the monitor is plugged in:
And here is when I unplug the VGA monitor:
And here it is plugged back in:
Method #2 - cat sysfs
If you poke around under
/sys
the display ports that you have are listed here under/sys/class/drm
.The display ports for monitors will be
card0-DP-1
(VGA) and thecard0-HDMI-A-1
(HDMI). If you look in each of these directories, there's a file calledstatus
. If youcat
this file you can see the port's status and whether a monitor is connected or not.Same test as above, plugged in:
Unplugged:
References