How to obtain a list of running emacs servers from a shell?

1.5k views Asked by At

I want to write a script that "links" an emacs server to a certain directory. To do so I need to check all existing servers to be sure if a server with a specific name is running or not. Then the script can decide to start a new server, or not, before opening a file from that directory using emacsclient.

I have been looking around to find out how to list the existing emacs running servers, unsuccessfully though.

Is there anything like emacs --list-servers that I could use?

Cheers.

2

There are 2 answers

10
AudioBubble On BEST ANSWER

Emacs stores the socket files for all running servers in ${TMPDIR}/emacs$(id -u), where $TMPDIR defaults to /tmp if unset. Find all running servers is as simple as listing all sockets in that directory.

In Bash, this would look roughly like

local serverdir="${TMPDIR:-/tmp}/emacs${UID}"
local -a servers
for file in ${serverdir}/*; do
  if [[ -S ${file} ]]; then
    servers+=("${file##*/}")  
  fi
done

echo "${servers[@]}"

The servers array now contains the names of all running Emacs servers, for use with emacslcient -s.

Edit: Since you're using Python apparently, the same in Python:

import os
from stat import S_ISSOCK

serverdir = os.path.join(os.environ.get('TMPDIR', '/tmp'),
                         'emacs{0}'.format(os.geteuid()))
servers = [s for s in os.listdir(serverdir)
           if S_ISSOCK(os.stat(os.path.join(serverdir, s)).st_mode)]
0
Mastergamer433 On

If i didnt misunderstood you, you could just use emacsclient -a emacs. This will start emacs if a emacs server is not running else open emacsclient and connect.