Given the additional info, I guess that your emacsclient is connecting to the "wrong" emacs server. (Or better, the first one that was started: subsequent invocations of
emacs --daemon will fail to start the server since the communication socket is already in use.) If the emacs daemon was started in a previous X session, then it's using the wrong credentials for connecting to the X display and thus fails.
You can find out which emacs process is running the server by connecting to it in non-graphics/tty mode; run emacsclient in a terminal with the
You can kill a running emacs by having it run LISP code through emacsclient:
emacsclient -t --eval '(progn (server-save-buffers-kill-terminal 1) (save-buffers-kill-emacs 1))'
-t option (alias for
--tty) is to avoid Emacs connecting to the X display;
server-save-buffers-kill-terminal detaches the emacsclient before you tell Emacs to stop (otherwise it will issue a confirmation prompt);
save-buffers-kill-emacs function is what is normally invoked by
C-x C-c, argument
1 tells Emacs not to ask for confirmation.
In addition, I guess the reason you are having so many
emacs --daemon running is that you invoke emacsclient with the
--alternate-editor="" option: the man page emacsclient(1) states that:
If the value of (the alternate) EDITOR is the empty string, then Emacs is started in daemon mode and emacsclient will try to connect to it.
It could be a better option to start
emacs --daemon from your X session startup script (e.g.,
.gnomerc or the GNOME session configutation) so that the session manager will take care of killing the emacs daemon when the session terminates.