7

I am starting the emacs server using

systemctl --now --user enable emacs

That server is running, if I run

systemctl --user status emacs

I got

$ systemctl --user status emacs
● emacs.service - Emacs text editor
   Loaded: loaded (/usr/local/lib/systemd/user/emacs.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-06-09 09:27:56 CDT; 2 days ago
     Docs: info:emacs
           man:emacs(1)
           https://gnu.org/software/emacs/
 Main PID: 17613 (emacs)
   CGroup: /user.slice/user-1000.slice/user@1000.service/emacs.service
           └─17613 /usr/local/bin/emacs --fg-daemon

But, when I try to connect using emacsclient -c some_file.py

I got

$ emacsclient -t .psqlrc
emacsclient: can't find socket; have you started the server?
To start the server in Emacs, type "M-x server-start".

What am I missing?

EDIT:

Following the advice of @Tobias I ran

$ sudo netstat -xauopen | grep emacs

And the result was the following:

unix  3      [ ]         STREAM     CONNECTED     61841    17982/emacs          
unix  3      [ ]         STREAM     CONNECTED     62269    17982/emacs          
unix  3      [ ]         STREAM     CONNECTED     62268    17982/emacs   

I was expecting see more information, but this doesn't seem right. What is happening?

nanounanue
  • 125
  • 11
  • Please add which emacs version you are running. – Tobias Jun 11 '18 at 22:34
  • `GNU Emacs 27.0.50` – nanounanue Jun 12 '18 at 15:51
  • You also need to pass the `--now` option to [`systemctl --user enable emacs`](https://www.freedesktop.org/software/systemd/man/systemctl.html#enable%20UNIT%E2%80%A6) or you additionally need to run `systemctl --user start emacs`. But I assume that you already did something like that because of the run-status of emacs. – Tobias Jun 15 '18 at 13:38
  • Yup, but still I no luck connecting to it :( – nanounanue Jun 15 '18 at 17:11
  • I added `--now` to the options. – Tobias Jun 15 '18 at 17:21
  • It didn't work. I stopped the service. Added the `--now` argument i.e. `systemctl --user enable emacs --now`, and tried to connect using `emacsclient -t .psqlrc` and smae eeor – nanounanue Jun 15 '18 at 17:53
  • According to the [man page](https://www.linux.org/docs/man1/systemctl.html) the syntax is `systemctl [OPTIONS...] COMMAND [NAME...]`. So the option `--now` must be before the command `enable` as I wrote in my edit of the question. I don't think that this makes a difference. It is only to avoid potential trivial problem sources. Did you [check the server](http://blog.refu.co/?p=1296) with `sudo netstat -xaupen | grep emacs`? – Tobias Jun 15 '18 at 20:39
  • @Tobias considering the status reported by `systemd`, Emacs daemon is running already. –  Jun 15 '18 at 21:04
  • Maybe I missed something, but don't you need to start the server in your init file? Include something like `(require 'server)` and `(if (not (server-running-p)) (server-start))`. – Dan Jun 15 '18 at 22:29
  • @DoMiNeLa10 wrote: "Emacs daemon is running already." I know that. [The link I provided](http://blog.refu.co/?p=1296) says how to check which socket emacs is connected to. – Tobias Jun 15 '18 at 22:33
  • @Dan No you don't need to start the server in your init file. The command line option `--fg-daemon` should do that for you. – Tobias Jun 15 '18 at 22:34
  • @Tobias so Do I need to specify the port when connecting to the server? – nanounanue Jun 15 '18 at 22:36
  • You asked: "Do I need to specify the port when connecting to the server?" Just check it with the info at the link I provided. If the socket has the form `/tmp/$USER/server` (where `$USER` stands for your user name) you don't need to specify it. Maybe, the new form `/tmp/emacs1000/server` is also okay. I think the `1000` stands for the UID. Maybe you just try to specify the socket. But, I doubt that it makes a difference. – Tobias Jun 15 '18 at 22:44
  • If you use remote emacs clients there is a chance that you setup emacs to use tcp instead of sockets with `server-use-tcp`. In that case you need to specify the `--server-file`. The server files end up in `~/.emacs.d/server/` by default. – Tobias Jun 15 '18 at 22:54
  • @Tobias I added to the question the output of the command that you suggested and comparing that to the example provided in the link, it doesn't seem right. – nanounanue Jun 17 '18 at 05:10
  • @nanounanue You wrote "I added to the question the output of the command that you suggested...it doesn't seem right". I guess so too. Looks like you are using unix sockets after all. There should be at least one server process listening but all are connected in your case. Test: Make sure that no emacsen are running (`pkill emacs` and `pgrep emacs`). Start emacs normally and run `M-x server-start`. After all we saw up to now I almost expect an error message at that point. Something like Directory `/tmp/emacs1000` is unsafe. After fixing that try to connect with `emacsclient` to the server. – Tobias Jun 17 '18 at 17:34
  • I executed `pkill emacs` but that didn't work, I need to do `systemctl --user stop emacs` for getting all the emacs processes dead (i.e. empty `pgrep emacs` and empty `sudo netstat -xaupen | grep emacs`). After that just run `emacs` (But my `sudo netstat -xaupen | grep emacs` is similar to the previous one!). Anyway, I ran `M-x server start` (no error) and now the output is `unix 2 [ ACC ] STREAM LISTENING 2907744 8262/emacs /tmp/emacs1000/server` (plus several lines equal as before) and I was able to connect using emacsclient -t .psqlrc` ... I am very confused now – nanounanue Jun 17 '18 at 23:55
  • @nanounanue Next test: Kill all emacsen and try `emacs --fg-daemon` on the command line. Check with `netstat` (make sure one instance is LISTENING) and try to connect with `emacsclient`. If that works maybe the `systemd` method also works after cleaning up. – Tobias Jun 18 '18 at 08:57
  • @Tobias, I did the things that you suggested and no luck. I got te same output (`unix 3 ...`) nothing related to `LISTENING` . I also tried `emacs --bg-daemon` and same result. Any further ideas? – nanounanue Jun 19 '18 at 15:38
  • @nanounanue I guess `emacs --funcall=server-start` from the command line is working since `M-x server-start` works. Could we try to replace `/usr/bin/emacs --daemon` by `emacs --funcall=server-start` in the dbus unit-file `/usr/local/lib/systemd/user/emacs.service`? I think you can also create an alternative unit file at `~/.config/systemd/user/emacs.service`. Just take the system file as a template. That method has the disadvantage that it creates an initial Emacs frame. – Tobias Jun 19 '18 at 15:46
  • There was a change from 27.0.50 to 27.1: https://lists.gnu.org/archive/html/bug-gnu-emacs/2018-06/msg00445.html. They changed `Type` from `simple` to `notify`. Don't know whether that is related. – Tobias Jun 19 '18 at 16:06
  • I am upgrading, let's see if that works – nanounanue Jun 21 '18 at 17:02
  • Nope. Didn't work – nanounanue Jun 22 '18 at 16:45

2 Answers2

1

I'm using 24-25 emacs version as daemon.

 cat /etc/systemd/user/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)"
 Environment=SSH_AUTH_SOCK=%t/keyring/ssh
 Restart=always

 [Install]
 WantedBy=default.target

With 26.1+ add options --fg-daemon and --bg-daemon. Try old --daemon alias --bg-daemon fork unit. Maybe work.

Also add to emacsclient -a '' -t.

AfsmNGhr
  • 111
  • 4
0

I'm having the same issue. I came up with a script to start emacsclient that is working fine for me:

#!/bin/sh

SOCKET=/tmp/emacs$UID/server
PARAMS="--socket-name=$SOCKET"

if [[ $TMUX || $SSH_CLIENT ]]
then
    PARAMS+=" -t"
else
    PARAMS+=" -c"
fi

exec /usr/bin/emacsclient $PARAMS "$@"

Unfortunately, this doesn't seem to be the best solution since it won't fix launching emacsclient directly (either in commandline or in the desktop file). However, I have a key shortcut to launch it using the script.

aiguofer
  • 588
  • 3
  • 14
  • Alternatively, based on https://stackoverflow.com/a/28553846/1815486, you can use `SOCKET=$(lsof -c emacs | grep $USER | grep server | grep STREAM | awk '{print $9}')` to find it regardless of where it is (in case you've set it elsewhere) – aiguofer Nov 30 '18 at 17:29