Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: Writing a dinit user service for the emacs daemon (Read 285 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Writing a dinit user service for the emacs daemon

Hello all,

I am attempting to write a dinit user service for the emacs daemon.  What I have so far successfully starts the daemon, but then registers it as "stopped" even though it is running.  I suspect that dinit is not receiving the correct PID after emacs forks itself.


Here are the contents of "~/.config/dinit.d/emacs" :

Code: [Select]
type = bgprocess
command = /usr/bin/emacs --daemon
restart = no
pid-file = ~/.emacs.d/emacs.pid

The pid-file "~/.emacs.d/emacs.pid" is generated by the following elisp in "~/.emacs.d/early-init.el" :
Code: [Select]
 elisp
(with-temp-file "~/.emacs.d/emacs.pid"
    (insert (number-to-string (emacs-pid))))

I am assuming that dinit reads the file before it has been updated, so it does not know to which PID emacs is actually running.  Any suggestions?

Thank you!

Re: Writing a dinit user service for the emacs daemon

Reply #1
Not sure how it interprets
Code: [Select]
~/.emacs.d
; have you tried putting the whole path, just to exclude this possibility ?
Also, why
Code: [Select]
type = bgprocess
and not
Code: [Select]
type = process
?

Re: Writing a dinit user service for the emacs daemon

Reply #2
Quote
I am assuming that dinit reads the file before it has been updated, so it does not know to which PID emacs is actually running.  Any suggestions?

Sounds like the pid file is written by the forked child instead of the parent, so that's broken behaviour on part of emacs. I guess you could try filing a bug (but the maintainers might argue it, or ignore it).

The --fg-daemon option looks like it avoids forking, so you could use type = process, which is always preferred. Any reason why you didn't do that?

 

Re: Writing a dinit user service for the emacs daemon

Reply #3
Quote
I am assuming that dinit reads the file before it has been updated, so it does not know to which PID emacs is actually running.  Any suggestions?

Sounds like the pid file is written by the forked child instead of the parent, so that's broken behaviour on part of emacs. I guess you could try filing a bug (but the maintainers might argue it, or ignore it).

The --fg-daemon option looks like it avoids forking, so you could use type = process, which is always preferred. Any reason why you didn't do that?

Thank you for your help - that was the right solution.  I didn't use --fg-daemon originally simply from ignorance.



And indeed, that is what the equivalent systemd emacs.service does, which I should have referenced originally:
Code: [Select]
[Unit]
Description=Emacs text editor
Documentation=info:emacs man:emacs(1) https://gnu.org/software/emacs/

[Service]
Type=simple
ExecStart=/usr/bin/emacs --fg-daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"
Environment=SSH_AUTH_SOCK=%t/keyring/ssh
Restart=on-failure

[Install]
WantedBy=default.target
Reference: https://emacsredux.com/blog/2020/07/16/running-emacs-with-systemd/


My ~/.config/dinit.d/emacs is now:
Code: [Select]
type = process
command = /usr/bin/emacs --fg-daemon
stop-command = /usr/bin/emacsclient --eval "(kill-emacs)"
restart = no



Thank you for your work on dinit - I believe it is currently one of the most important projects in the entire Linux/BSD ecosystem.