Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 为外部显示添加udev规则_Linux_Udev_Xrandr - Fatal编程技术网

Linux 为外部显示添加udev规则

Linux 为外部显示添加udev规则,linux,udev,xrandr,Linux,Udev,Xrandr,我编写了一个小shell脚本,用xrandr配置附加的外部显示器 # cat /home/didi/bin/monitor_autoswitcher.sh #!/bin/bash xrandr | grep "HDMI1 connected" if [[ $? == 0 ]]; then # is connected xrandr --output HDMI1 --right-of LVDS1 --auto else # not connected xrandr --outp

我编写了一个小shell脚本,用xrandr配置附加的外部显示器

# cat /home/didi/bin/monitor_autoswitcher.sh 
#!/bin/bash

xrandr | grep "HDMI1 connected"
if [[ $? == 0 ]]; then
  # is connected
  xrandr --output HDMI1 --right-of LVDS1 --auto
else
  # not connected
  xrandr --output HDMI1 --auto
fi

xrandr | grep "VGA1 connected"
if [[ $? == 0 ]]; then
  # is connected
  xrandr --output VGA1 --right-of LVDS1 --auto
else
  # not connected
  xrandr --output VGA1 --auto
fi
这很有效。现在我想让它自动触发,并发现这可以用udev完成。 我试过了

当插入外部电源时,显示输出

KERNEL[465828.240250] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [465828.243549] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
当你把它拔出来的时候

KERNEL[465836.844209] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [465836.847445] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
也很好

然后我添加了一个udev规则:

# cat 40-external-display.rules 
SUBSYSTEM=="drm", ACTION=="change", RUN+="/home/didi/bin/monitor_autoswitcher.sh"
然后重新启动udev

service udev restart
不幸的是,在插入/拔出显示器时仍然没有发生任何事情。脚本
monitor\u autoswitcher.sh
肯定能工作,因为在插入后手动调用它会完成它应该做的事情


少了什么?

这看起来几乎一样。我看到的唯一真正的区别是脚本设置了DISPLAY变量,它可能是key


这是所缺少的一半。还需要:XAUTHORITY env变量,请参阅。我认为你的答案是正确的,因为它使我走上了正确的轨道。
service udev restart