Linux: How to run a script when an external monitor is connected?

6,140

Solution 1

There are a set of tools called read-edid that can parse extended display identification data (EDID). If your external monitor is actually detected by these tools, you might be able to periodically check for detection and use the output to start scripts, etc.

You didn't mention though whether you need to do this regardless of whether X was running.

Solution 2

You should use udev events... a possible rule might look like:

KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/ninette/.Xauthority", RUN+="/usr/local/bin/0x_hw_hotplug_monitor.sh"

(Source https://bbs.archlinux.org/viewtopic.php?pid=1329375#p1329375)

Solution 3

I was looking for the same answer :/
I'm using disper (http://willem.engen.nl/projects/disper) and gconftool to enable/disable my second monitor together with a second gnome-panel on it.
disper -l lists all connected monitors, so you could poll that as a last resort.

# only primary display
disper -s
gconftool-2 --type int --set  /apps/panel/toplevels/panel_1/screen -- -1


# dual display (nvidia twinview)
disper -e
gconftool-2 --type int --set  /apps/panel/toplevels/panel_1/screen 0
killall gnome-panel
Share:
6,140
Joernsn
Author by

Joernsn

Updated on September 18, 2022

Comments

  • Joernsn
    Joernsn over 1 year

    How can I detect when an external monitor is connected in linux?

    I need to run various scripts to set up my workspace.

    Is there some clever way to kick off a script when the monitor state has changed?

    I'm using the Nvidia display driver.

    • new123456
      new123456 about 13 years
      The hackish way would be to write a script that queries for changes in output in xrandr every couple seconds, and runs the script(s) if it notices a difference.
    • Andy
      Andy almost 13 years
      Ideally you can tell udev to run a script. I don't expect that to work with nvidia drivers until KMS is supported, although I haven't tried. stackoverflow.com/questions/5469828/…