Solved Running Emacs as a daemon

FDreamer

Member

Reaction score: 8
Messages: 26

Hi guys,
I would like to start emacs as a daemon at boot, using the configuration file of an user named "alex".
Here is my rc.dscript, in /usr/local/etc/rc.d/emacsd

Bash:
#!/bin/sh

# PROVIDE: emacsd
# REQUIRE: login # after login

# Emacs daemon
. /etc/rc.subr

name="emacsd"
rcvar=emacsd_enable

start_cmd="${name}_start"
stop_cmd="${name}_stop"
load_rc_config $name
: ${emacsd_enable:=no}
: ${_msg="Emacs daemon started."}

emacsd_start()
{
    su alex -c "env HOME=/home/alex /usr/local/bin/emacs --daemon"
}
emacsd_stop()
{
    su alex -c "/usr/local/bin/emacsclient --eval \"(kill-emacs)\""
}
run_rc_command "$1"

When the daemon is started at boot, my configuration files is not loaded (instead of Doom emacs, I'm back to default Emacs). If I start the daemon manually with service emacsd start, it works fine.

It seems the daemon cannot access the configuration file during boot. Any ideas ?
Thanks in advance
 
OP
F

FDreamer

Member

Reaction score: 8
Messages: 26

Answering my own question : the fix is to pass the -u flag, like this :

Code:
emacsd_start()
{
    su alex -c " /usr/local/bin/emacs --daemon -u alex"
}

Edit
To avoid waiting for emacs during boot, I now use crontab to start the daemon in the background. Much easier :)

Code:
@reboot /usr/local/bin/emacs --daemon
 
Last edited:

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,345
Messages: 38,863

Add a : ${emacsd_user:=alex} and replace each occurrence of alex with ${emacsd_user}. Now it's configurable by setting the user in rc.conf: emacsd_user="someoneelse"
 
Top