通过udev规则的python脚本用于热插拔显示端口监视器

通过udev规则的python脚本用于热插拔显示端口监视器

我在 ubuntu 安装上运行 i3-wm。当我(断开)连接 miniDP 电缆时,i3 不会自动检测和配置显示器设置。我尝试实现一个运行 python 脚本的 udev 规则,该脚本又执行 xrandr 命令。我可以从命令行运行该脚本并且它可以工作,但当我拔掉 mDP 电缆时它似乎无法运行。以下是我的 udev 规则的内容,位于 /etc/udev/rules.d/monitor.rules 中:

KERNEL=="card0", SUBSYSTEM=="drm", ENV{XAUTHORITY}="/var/run/gdm/auth-for-ME-OlbTje/database", RUN+="/path/to/my/script/monitor_toggle.py"

根据评论中的建议,我添加了来自以下代码的日志记录这里。该脚本现在运行,我可以通过 /var/log/syslog 查看我的日志信息,但是......一百万个进程开始运行并挂起系统。我必须硬重置机器。这是有问题的脚本。

import subprocess
import logging
from logging.handlers import SysLogHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
syslog = SysLogHandler(address = '/dev/log')
logger.addHandler(syslog)

home_dual       = "/home/vazquez/.screenlayout/i3_home_dual.sh"
home_single     = "/home/vazquez/.screenlayout/i3_home_single.sh"
monitor_toggle  = "/home/vazquez/bin/monitor_toggle.py"


def main():
    logger.debug("In Main")
    card = "/sys/class/drm/card0-DP-1"
    logger.debug("Checking status of mDP")
    status = subprocess.check_output(["cat","/sys/class/drm/card0-DP-1/status"] )
    status = status.decode( 'ascii' )
    logger.debug("status: {0}".format(status))

    # If the mDP is disconnected run home_single
    if 'disconnected' in status:
        # ret = subprocess.check_output(monitor_toggle)
        logger.debug("montor")
        try:
            dead = subprocess.check_output(["sh", home_single])
        except:
            logger.debug( "ret1 = {0}".format(dead) )
    # else the mDP is connect, run home_dual    
    else:
        logger.debug("In else")
        logger.debug("connecting external monitor")
        # ret = subprocess.check_output(monitor_toggle)
        try:
            beef = subprocess.check_output(["sh", home_dual])
        except:
            logger.debug(" ret 2: {0}".format(beef) )


if __name__=="__main__":
    main()

/var/log/syslog 输出

tail  /var/log/syslog
Apr 26 21:35:01 vazquez-dev CRON[3243]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Apr 26 21:36:11 vazquez-dev In Main
Apr 26 21:36:11 vazquez-dev Checking status of mDP
Apr 26 21:36:11 vazquez-dev status: disconnected
Apr 26 21:36:11 vazquez-dev montor 
Apr 26 21:39:34 vazquez-dev In Main
Apr 26 21:39:34 vazquez-dev Checking status of mDP
Apr 26 21:39:34 vazquez-dev status: connected
Apr 26 21:39:34 vazquez-dev In else
Apr 26 21:39:34 vazquez-dev connecting external monitor

相关内容