Emacs can be run as a daemon using a flag:
I learned a bit about rc.d scripting already by reading Practical rc.d scripting in BSD [1]. There also seems to be exactly one prior thread on the forums that touches this very topic although somewhat differently [2]. A certain post on reddit can also be used as a guidance [3]. I tinkered with the templates in these sources and put together a script, but it has problems and I would appreciate input from anyone literate in the matter.
The idea is to have a working Emacs daemon, which comes down to
I present what I was able to patch together below. A discussion follows.
Discussion:
1) I try to make use of daemon(8) as in [3][9]. This introduces the problem of pid file, which cannot be accessed without elevated privilege and I am not sure what the good practice dictates here: specifying the
2)
3) Related to that, in [2] and [4] the user substitution is handled within that same method with
4) More generally, I am wondering if there may be an elegant way to specify the
That's more or less it. While my example concerns an emacs server specifically, the questions are of universal nature, so perhaps it can be of some use to others interested in service scripts. I include sources mentioned in the text above, as well as a reference to applicable manuals.
Resources:
[1] Practical rc.d scripting in BSD
[2] Running Emacs as a daemon
[3] Supervised FreeBSD rc.d script for a Go daemon
[4] How to run a daemon as a user that isn't root?
[5] rc(8)
[6] rc.conf(5)
[7] rc.subr(8)
[8] rcorder(8)
[9] daemon(8)
emacs --daemon
. Then, all subsequent calls of emacsclient
should connect to the server, saving startup time. FreeBSD's Emacs package doesn't seem to include any rc.d
scripts so it's up to the user to procure one and that's what I'm trying to do.I learned a bit about rc.d scripting already by reading Practical rc.d scripting in BSD [1]. There also seems to be exactly one prior thread on the forums that touches this very topic although somewhat differently [2]. A certain post on reddit can also be used as a guidance [3]. I tinkered with the templates in these sources and put together a script, but it has problems and I would appreciate input from anyone literate in the matter.
The idea is to have a working Emacs daemon, which comes down to
emacsclient
being able to connect to the running daemon. The service should be ran as a user (current unprivileged user or dedicated emacs user, perhaps?), to avoid privilege issues (I don't know if it's the case in general or a result of the security.bsd.see_other_uids=0
hardening option, but my unprivileged user running the emacs client can't see the emacs socket of the daemon started as root).I present what I was able to patch together below. A discussion follows.
Bash:
#!/bin/sh
# PROVIDE: emacsd
# REQUIRE: LOGIN
. /etc/rc.subr
name="emacsd"
rcvar="emacsd_enable"
emacsd_command="/usr/local/bin/emacs"
emacsd_flags="daemon"
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
command_args="-P ${pidfile} -r -f -u ${emacsd_user} ${emacsd_command}"
stop_postcmd="emacsd_exit"
load_rc_config $name
: "${emacsd_enable:="NO"}"
: "${emacsd_user:="artur"}"
emacsd_exit()
{
[ -e "${pidfile}" ] && \
echo "Stopping supervising daemon." && \
kill -s TERM "$(cat ${pidfile})"
}
run_rc_command "$1"
Discussion:
1) I try to make use of daemon(8) as in [3][9]. This introduces the problem of pid file, which cannot be accessed without elevated privilege and I am not sure what the good practice dictates here: specifying the
pidfile
variable somewhere in emacsd_user
Home dir? Will it be safe or suffice to get it running? Perhaps I should just give up on daemon(8) and the pidfile completely?2)
emacsd_command
: following the recommended practice I include daemon
in the emacsd_flags
variable. This is different than in [2] and [4], where the users are advised to use a dedicated ${name}_start()
method that includes the --daemon
and other flags.3) Related to that, in [2] and [4] the user substitution is handled within that same method with
su
. The manual page for daemon(8) shows that it has -u
flag, that substitutes the user. The manual mentiones potential subsitute privileges that may be needed (as they in fact seem to be in case of pidfile). According to [6] emacsd_user
also specifies who to run a service as. I am at a loss, which of those options should be specified and used.4) More generally, I am wondering if there may be an elegant way to specify the
"${emacsd_user:="artur"}"
to be more universal? I figure that with "${emacsd_user:="$(id -un)"}"
the expansion happens when the script is run, that is on system startup, so it goes to... root? Again, that's not really a problem to me, as I know to specify it in /etc/rc.conf, but I curious if it could be made more general (root? emacsuser?).That's more or less it. While my example concerns an emacs server specifically, the questions are of universal nature, so perhaps it can be of some use to others interested in service scripts. I include sources mentioned in the text above, as well as a reference to applicable manuals.
Resources:
[1] Practical rc.d scripting in BSD
[2] Running Emacs as a daemon
[3] Supervised FreeBSD rc.d script for a Go daemon
[4] How to run a daemon as a user that isn't root?
[5] rc(8)
[6] rc.conf(5)
[7] rc.subr(8)
[8] rcorder(8)
[9] daemon(8)