My problem is very similar to this one from Unix & Linux Exchange.
I use systemd to start emacs --daemon
as my user account. Recently I have had the following problem when trying to open a new frame of my emacs process.
$ emacsclient -c
Waiting for Emacs...
*ERROR*: Display :0 can't be opened
In the question linked above, the user had old emacs daemons still running, but I only have one.
$ ps ux | grep [e]macs
anthony 675 0.0 1.2 432560 103680 ? Ssl 20:41 0:03 /usr/bin/emacs --daemon
I imagine my problem is related to $XAUTHORITY
as suggested in the second answer of the question linked above, but I have been unable to solve the issue on my own and was hoping someone here could help. Possibly relevant information to follow:
$ echo $XAUTHORITY
/var/run/gdm/auth-for-anthony-ATsjnA/database
$ ls -l /var/run/gdm/auth-for-anthony-ATsjnA/database
-rw------- 1 anthony anthony 106 Feb 18 20:41 /var/run/gdm/auth-for-anthony-ATsjnA/database
$ ps -C emacs wwe
PID TTY STAT TIME COMMAND
675 ? Ssl 0:02 /usr/bin/emacs --daemon DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DISPLAY=:0 HOME=/home/anthony LANG=en_US.UTF-8 LOGNAME=anthony MAIL=/var/spool/mail/anthony
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin SHELL=/bin/bash USER=anthony
XDG_RUNTIME_DIR=/run/user/1000 MANAGERPID=667
I log in with gdm into gnome-shell, and the emacs process is started with systemctl --user start emacs.service
. Here's the contents of the emacs.service
:
[Unit]
Description=Emacs: the extensible, self-documenting text editor
[Service]
Type=forking
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
Restart=always
[Install]
WantedBy=default.target
Any help would be greatly appreciated.
Edit
At politza's suggestion:
$ DISPLAY=:0.0 emacsclient -c
Waiting for Emacs...
*ERROR*: Display :0.0 can't be opened
I connect to the server fine in gnome-terminal with emacsclient -nw
as well.
Second Edit
At Stefan's suggestion, I edited my ~/.xsessions
to the following
gdmauth=$XAUTHORITY
unset XAUTHORITY
export XAUTHORITY
xauth merge "$gdmauth"
/bin/bash --login -i ~/.xinitrc
but I still have the same issue. I'm not sure if it matters, but it seems the value of XAUTHORITY changes with every log-in. I don't know when that value is assigned so that the script above will work.
Manually running xauth merge /var/run/gdm/auth-for-anthony-<random>/database
finishes silently, but I still run emacsclient.