On distributions with init system systemd the output of
systemctl --user show-environment
shows DISPLAY and XAUTHORITY. This is at least true for my debian 9 system with systemd and gdm3 as display manager.
Pitfall: After running startx xterm -- :2 vt2 on tty2 systemctl gave me DISPLAY and XAUTHORITY from the new display; my main display :0 was no longer observably this way.
Other approaches:
At least for XAUTHORITY it is more reliable to parse the output of ps aux | grep Xorg and look for option -auth. In my case it is located at /run/user/1000/gdm/Xauthority and not at ~/.Xauthority.
Pitfalls:
- There can be more than one Xorg instance.
- Instead of Xorg there can be
Xwayland.
- Instead of Xorg there can be
Xvfb or something else.
- It may be a remote session without local Xorg at all.
Normally Xorg commands also contain the display number. Unfortunately, mine does not:
/usr/lib/xorg/Xorg vt1 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
-displayfd 3 somehow points to DISPLAY. If looking as root at /proc/$(pidof Xorg)/fd/3 I find
lrwx------ 1 root root 64 Mär 8 22:45 3 -> socket:[21437]
Though, I don't know how to look for socket 21437. I am sure it points to /tmp/.X11-unix/X0. One approach is lsof +E -aUc Xorg that shows interesting output: many connections containing @/tmp/.X11-unix/X0 and one dbus connection.
A bit dirty: The fd folder also shows me
l-wx------ 1 root root 64 Mär 8 22:45 5 -> /var/log/Xorg.0.log
Xorg.0.log is a clear indication for display :0.
Another approach: notify-send seems to gather DISPLAY and XAUTHORITY with some dbus magic. But I have no clue how. That may be the cleanest and most portable way, if at least a dbus daemon is running.
ssh -X)? – Daniel Pryden Mar 08 '18 at 20:36XAUTHORITY(tested withzenityincron) – Gilles Quénot Mar 08 '18 at 22:07xhostCheck the output ofxhost, remove the entry withxhost -SI:localuser:USERNAMEand try again.xhostentries can be given, but do not rely on them. – mviereck Mar 08 '18 at 22:10XAUTHORITYto what I fetched from Xorg command or other places, in cron, I get error 'Unable to init server: Could not connect: Connection refused' (with the good DISPLAY and the same user) – Gilles Quénot Mar 08 '18 at 22:26XAUTHORITYbut differences in network or hostname, not sure why this happens to you. Check https://stackoverflow.com/a/25280523/5369403, try to insertffffinto the cookie. Like:xauth nlist :0 -f $XAUTHORITY | sed -e 's/^..../ffff/' | xauth -f /tmp/mycookie nmerge -and afterwards useXAUTHORITY=/tmp/mycookie– mviereck Mar 08 '18 at 22:40tr '\0' '\n' < /proc/<PID>/environand I need to automating fetching this as user and with n > 1 Xorgs instances – Gilles Quénot Mar 08 '18 at 23:56