Enabeling emacs.service => "*ERROR*: Display :0 can’t be opened"

I’m using Emacs all the time. So I tried to start it as a daemon:


systemctl --user enable emacs.service
Created symlink /home/AW/.config/systemd/user/default.target.wants/emacs.service → /usr/lib/systemd/user/emacs.service.
AW@linux-izun:~> systemctl --user start emacs.service

For other reasons I had to reboot. Then:


emacsclient -c &
[1] 4047
AW@linux-izun:~> *ERROR*: Display :0 can’t be opened
[1]+  Exit 1                  emacsclient -c

The content of emacs.service is:


[Unit]
Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

[Service]
Type=notify
ExecStart=/usr/bin/emacs --fg-daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
# The location of the SSH auth socket varies by distribution, and some
# set it from PAM, so don't override by default.
# Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=on-failure

[Install]
WantedBy=default.target



What is wrong with my setup?

IMHO some information is missing here. Please always copy/paste into a post a complete set: prompt/command line, output, next prompt. Then we can see exactly what “you” did, we know who “you” are (root or user), your wrking directory, and that it is complete.

The first CODE block e.g. does not start with a prompt. There is one later in that block in this case, but it is easier for everybody if it start also with it.

And the contents of your service file in the third CODE block also has no command (a cat I presume) with the result that nobody knows if it is in the correct place.

The second block also is incomplete. Is this command done from a terminal emulator (konsole or similar) inside a GUI session? I ask, because the error normally is shown when a program is started that wants to open a window, but no GUI session is attached to it, or the owner of the GUI session to be used did not allow access.

Also, you say there was a new boot and that implies of course a new login. Did you check if that started the deamon for the user?

Sorry if I cut a few “AW@linux-izun:~>”.

The first CODE block e.g. does not start with a prompt. There is one later in that block in this case, but it is easier for everybody if it start also with it.

And the contents of your service file in the third CODE block also has no command (a cat I presume) with the result that nobody knows if it is in the correct place.

I’m not sure I understand, but the third code block is the file /usr/lib/systemd/user/emacs.service

The second block also is incomplete. Is this command done from a terminal emulator (konsole or similar) inside a GUI session? I ask, because the error normally is shown when a program is started that wants to open a window, but no GUI session is attached to it, or the owner of the GUI session to be used did not allow access.

The second block is from yakuake, so yes, from something similar to konsole.

Also, you say there was a new boot and that implies of course a new login. Did you check if that started the deamon for the user?

Yes, after loging in there was a process “emacs-gtk”.

Wait, what? I’ll be right back.

I had forgotten that I compiled and installed emacs myself in /usr/local , because of some flaws of the TW rpm weeks ago. I’m not sure what happened, but one mistake by me was that I didn’t adapt the path to emacs inside emacs.service. However, in the end I deleted the version under /usr/local/ and now I get a emacs daemon after logging in.

Thank you very much for your help. My apology, I forgot about the self compiled version.

That is allright. It is part of the helping process. Just asking stupid questions to make the OP rethink things.

I experienced that very often in my career. Letting people explain their program often ends in a: “Oh, how stupid of me!” The fact that you have to explain lets you avoid the jumps you make “because that is logical”. No need to say that after they walked off happy, I still did not know what the error was, or even what their program was supposed to do. rotfl!

Finally. I deleted my self compiled version of Emacs, but still starting an Emacs frame resulted more or less often in the error Display :0 or :1 can’t be opened, if I started Emacs soon after log in.

I got it fixed with adding this line to the file /usr/lib/systemd/user/emacs.server:

ExecStartPre=sleep 20

My guess is that systemd starts the emacs server too early after the user logs in.

So the whole file now consists of these lines:

[Unit]
Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

[Service]
Type=notify
ExecStartPre=sleep 20
ExecStart=/usr/bin/emacs --fg-daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
# Environment=DISPLAY=:%i
# The location of the SSH auth socket varies by distribution, and some
# set it from PAM, so don't override by default.
# Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=on-failure

[Install]
WantedBy=default.target

I’m so lucky. Even if something doesn’t work, I can have a look myself ! Free software rocks!!!