tags:

views:

83

answers:

1

I'd like to execute some code in my init file only if emacs server is running (specifically if emacs is started with the --daemon flag). There doesn't seem to be any hook that runs when server-start is called and there's no variable I can look at to see if the server is running.

A hack is to use (featurep 'server), since the server feature is not loaded unless the server is started, and this does seem to work for my purposes, but I'd like to know what the right way of doing this is. Thanks.

+8  A: 

If a server process is running, the associated process object is server-process. Testing if server-process is non-nil tells you whether the server is supposed to be running; you can test its status to check that it's in an acceptable state.

(and (boundp 'server-process)
     (memq (process-status server-process) '(connect listen open run)))

You can test whether Emacs was invoked as a daemon with (daemonp).

Gilles
`daemonp` is exactly what I was looking for, thanks!
pheaver