2

I'm running i3-wm on a ubuntu install. i3 doesn't auto-detect and configure the monitor set up when I (dis)connect the miniDP cable. I've tried implementing a udev rule that runs a python script which in turn executes xrandr commands. I can run the script from the command line and it works, but it doesn't seem to run when I unplug the mDP cable. Here is the contents of my udev rules which is located in /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"

Per suggestions in the comments, I added logging with code from here. The script runs now and I can see my logging info via /var/log/syslog but..... a million processes start running and hang the system. I have to hard reset the machine. Here is the script in question.

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 output

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
  • You say that "it doesn't seem to run". If you put a logging line at the top of the script that writes to the syslog - or even to a file in /tmp, does it definitely (not) run? – Chris Davies Apr 26 '16 at 20:16
  • Chances are that your script is running but failing. Make it log to a file and check the errors. If you need help, post the script and the log. – Gilles 'SO- stop being evil' Apr 26 '16 at 23:36
  • One obvious thing is that it makes no sense to set XAUTHORITY but not DISPLAY. This may not be the only problem however. You'll need to be more clever finding out XAUTHORITY since it changes every time you reboot, see http://unix.stackexchange.com/questions/10121/open-a-window-on-a-remote-x-display-why-cannot-open-display/10126#10126 – Gilles 'SO- stop being evil' Apr 26 '16 at 23:37
  • @Gilles That part confused me. I read something about xauth and display in an arch wiki and just threw it in there. I'm not sure what it's supposed to do though. – user3577138 Apr 27 '16 at 03:59
  • The question I linked explains about DISPLAY and XAUTHORITY. Why are you reading a file by calling cat?? From the logs, it looks like everything is fine until the home_dual script. Post all the scripts. – Gilles 'SO- stop being evil' Apr 27 '16 at 09:28

0 Answers0