1

I am able to get this udev rule in 99-monitor-hotplug.rules to trigger:

ACTION=="change", SUBSYSTEM=="drm", ENV{HOTPLUG}=="1", 
RUN+="/usr/local/bin/monitor-hotplug.sh"

But I cannot seem to get it to trigger an OpenCV GUI application in the monitor-hotplug.sh script.

I understand fundamentally the udev rule runs as root but no matter what syntax I try I cannot get it to run properly at the user level for running the application (the actual script to run the application works fine).

I have tried in RUN this format:

su - your_X_user_here -c 'export DISPLAY=:0; bash -c "/path/to/script.sh"'

with script:

#!/bin/bash
#sleep 5
date >> /var/log/opencvlog.log
cd ~/Downloads
./displayimage /home/<username>/Pictures/picture.png 
>/var/log/application.log 
2>&1

Another attempt:

Adding in 99-monitor-hotplug.rules to the current syntax:

ACTION=="change", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", 
ENV{XAUTHORITY}="/home/<username>/.Xauthority" ENV{HOTPLUG}=="1", 
RUN+="/usr/local/bin/monitor-hotplug.sh"

then in the actual script:

export DISPLAY=:0
export XAUTHORITY=/home/<username>/.Xauthority
cd ~/Downloads
date 
./displayimage /home/<username>/Pictures/picture.png

None of this is working, any thoughts on how to get this to work?

Thanks

Mr OpenCV
  • 51
  • 6

1 Answers1

0

When using display managers like gdm the current X authority file might not be in the user home directory, but in runtime directories like /run or /var/run.

You may try something like:

USER=<username>
export XAUTHORITY=$(find /var/run/gdm3/ -type f -path "*${USER}*" 2> /dev/null)

Newer gdm versions seem to put the file in a more generic location:

export XAUTHORITY=$(find /run/user/$(id -u "$USER")/ -name Xauthority 2> /dev/null)

I used this technique to call xrandr to adjust the screen resolution from a udev rule:
https://git.ao2.it/libam7xxx.git/blob/HEAD:/contrib/am7xxx-autodisplay.sh

  • Thanks for your response, I think what you are saying makes sense. I am using lightdm window manager, I think this is the correct path for Xauthority: XAUTHORITY=/var/run/lightdm/root/:0 . Even with this my script for gui application doesn't trigger based on the udev rule. Does this Xauthority value appear correct to you? Thanks – Mr OpenCV Dec 03 '18 at 15:34
  • I would expect something like `/var/run/lightdm/$USER/xauthority` check if something like this is available *after* `$USER` has logged in. It might be that this feature is not enabled by default by LightDM, see https://bugs.launchpad.net/ubuntu/+source/lightdm/+bug/1648107/comments/4 and https://askubuntu.com/questions/960793/whats-the-right-place-to-set-the-xauthority-environment-variable – Antonio Ospite Dec 04 '18 at 16:07