How to start emacs server only if it is not started?
Asked Answered
C

5

53

I'd like to use emacsclient to edit emails in Mutt.

I added this in .emacs

(server-start)

And in .muttrc I added

set editor="emacsclient -nw %s"

It seems they work. When I start a second Emacs, it complains there is already a server running so it issues errors. How to make sure to do (server-start) only if the server isn't already started?

Thanks

China answered 6/4, 2011 at 17:24 Comment(0)
O
54

The emacs daemon can be started automatically in a very simple manner. Just add this to your .bashrc/.zshrc/whatever

export ALTERNATE_EDITOR=""

Now when you invoke emacsclient (using either --tty or --create-frame) the server will be started (with emacs --daemon) if it's not already running.

I also find this shell alias handy:

alias e='emacsclient --tty'

Note that since Emacs 23 this is the preferred way to use Emacs in daemon mode. (start-server) is now mostly deprecated.

Olly answered 7/4, 2011 at 9:14 Comment(6)
This is a nice solution. emacsclient -c also works, and supports graphical clients. However, how would you go about opening a file in an existing emacs frame (say, in another terminal). Currently, I have a bash script called e that checks to see if the first argument is -o (for other) and runs emacsclient without -c if it does. Is there a better way, using the alias, that you can override the -t and cause an existing frame to open the file?Dorsoventral
@Dorsoventral not sure if you found your answer, but it sounds like you want the -t switch to emacsclient. It's a bit hard to tell because your comment seems to blur the distinction between a non-graphical emacs instance in a terminal, with a windowed graphical instance. -t is for the former, -c is for the latter.Demobilize
@Daniel: I got it sussed. Here is my script that I start emacs with. You can run it like this: $ e -o /path/to/file to send the file to another emacs already running elsewhere (which is useful). And it detects when it's being run inside a shell in emacs (the results are bad otherwise!). It always runs emacs in a terminal, although you can easily remove the -nw at the end to change this.Dorsoventral
Can you provide some reference for the claim that "this is the preferred way to use Emacs in daemon mode" and that "(start-server) is now mostly deprecated"? I don't see that mentioned in the most recent documentation, nor anywhere else. Thanks.Hocuspocus
Agreed i am not sure this is true, when i start emacs with -c or -t it actually tells me to start the server. "To start the server in Emacs, type "M-x server-start"." so that makes it sound like thats the preferred way.Donato
To add to the request for reference of best practice, I would love to have some more information about exactly why this works the way it does.Pamphlet
J
74

This code starts the server only if it's not running:

(load "server")
(unless (server-running-p) (server-start))
Joiejoin answered 6/4, 2011 at 19:35 Comment(4)
This is the only option that works on Windows currently since emacs --daemon says This platform does not support the -daemon flag.Gallaway
@legends2k, on Windows you can also do emacs --funcall server-start, but I agree this is better and works cross-platform.Hocuspocus
When I this lines to init.el, M-x returns Error reading from stdin. Is there any way to fix this problem?Cooney
I found that (load "server") resulted in errors when trying to use magit (in particular, on Windows). Using (require 'server) instead prevents these errors.Niall
O
54

The emacs daemon can be started automatically in a very simple manner. Just add this to your .bashrc/.zshrc/whatever

export ALTERNATE_EDITOR=""

Now when you invoke emacsclient (using either --tty or --create-frame) the server will be started (with emacs --daemon) if it's not already running.

I also find this shell alias handy:

alias e='emacsclient --tty'

Note that since Emacs 23 this is the preferred way to use Emacs in daemon mode. (start-server) is now mostly deprecated.

Olly answered 7/4, 2011 at 9:14 Comment(6)
This is a nice solution. emacsclient -c also works, and supports graphical clients. However, how would you go about opening a file in an existing emacs frame (say, in another terminal). Currently, I have a bash script called e that checks to see if the first argument is -o (for other) and runs emacsclient without -c if it does. Is there a better way, using the alias, that you can override the -t and cause an existing frame to open the file?Dorsoventral
@Dorsoventral not sure if you found your answer, but it sounds like you want the -t switch to emacsclient. It's a bit hard to tell because your comment seems to blur the distinction between a non-graphical emacs instance in a terminal, with a windowed graphical instance. -t is for the former, -c is for the latter.Demobilize
@Daniel: I got it sussed. Here is my script that I start emacs with. You can run it like this: $ e -o /path/to/file to send the file to another emacs already running elsewhere (which is useful). And it detects when it's being run inside a shell in emacs (the results are bad otherwise!). It always runs emacs in a terminal, although you can easily remove the -nw at the end to change this.Dorsoventral
Can you provide some reference for the claim that "this is the preferred way to use Emacs in daemon mode" and that "(start-server) is now mostly deprecated"? I don't see that mentioned in the most recent documentation, nor anywhere else. Thanks.Hocuspocus
Agreed i am not sure this is true, when i start emacs with -c or -t it actually tells me to start the server. "To start the server in Emacs, type "M-x server-start"." so that makes it sound like thats the preferred way.Donato
To add to the request for reference of best practice, I would love to have some more information about exactly why this works the way it does.Pamphlet
M
16

A bit of a late answer, but here is the solution that works for me. Whenever I start emacsclient, I use emacsclient -a '' -c The -a '' tells emacsclient to attempt to connect to an existing server, and if no server exists, start one then connect to it.

Metalwork answered 4/2, 2014 at 14:59 Comment(1)
The -a '' option is great. Note that -c always create a new frame. If typically want to edit the file in the existing emacs frame, so I omit this optionKeenakeenan
T
8

Avoid the problem alltogether via

emacs --daemon

in any shell or terminal so that Emacs runs in the background. That way emacsclient is always happy as there is always an Emacs server to connect to.

This being Emacs, there is also a function that starts the server only when needed but I can't quite recall its name right now. I use the --daemon option happily quite happily myself.

Towland answered 6/4, 2011 at 17:27 Comment(5)
I cannot agree more, the beauty of emacs --daemon shines when I put this in my standard Startup Applications Prefs in Ubuntu /usr/bin/emacs --daemon and whenever I startup my machine I can swiftly jot down a floating idea to my capture template in emacs by hitting Super + I (a keyboard shortcut to emacsclient -c --eval '(progn (org-capture) (delete-other-windows) (setq-local kill-buffer-hook (lambda () (delete-frame))))') and I get a nice emacs frame telling me what annoying ideas you want to get rid of off my head.Lorenzoloresz
should we run emacs --daemon as emacs --daemon & on the background?Cooney
I think it disappears by itself so if you say emacs --daemon you do get your prompt back. Try it, and follow-up with emacsclient -nw !Towland
emacsclient -nw keep returns emacsclient: connect: Connection refused error message not sure whyCooney
Then you have no daemon runnning.Towland
Z
1

Add this to your .bashrc/.zshrc

if ! ps -e -o args | grep -q '^emacs --daemon$'; then
  emacs --daemon
else
  echo "Emacs server Online"
fi

Update: now I prefer to use this line instead:

if ! ps -e -o args | grep -i 'emacs' | grep 'daemon'; then

because the process name will depend on your machine or the way you installed emacs.


Now your shell will start the deamon on startup, but only if it is not already running. Less wait time when you first run emacsclient -t, and it is faster than letting emacs --daemon check if it's already running.

As an alternative you could simply add:

eval 'emacsclient -e "(server-running-p)"'
Zajac answered 23/2, 2021 at 19:47 Comment(1)
I needed to use ps -xe -o args | grep -q '^emacs --daemon' on FreeBSD, because without the x option ps would not show emacs --daemon, and it also prints the version number, so I dropped the $ on the end.Subirrigate

© 2022 - 2024 — McMap. All rights reserved.