Skip to main content
Topic: Runit service for Emacs daemon (Read 1744 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Runit service for Emacs daemon

Hello all,

I really would like to have a runit service file to start the emacs daemon. Now, I imagine I could do this:

Code: [Select]
#!/bin/bash

emacs --daemon

Then, pop this script to /etc/runit/sv/emacs/run, chmod it and it would be all good to go. The only problem is, it would run as root. I haven't figured out how to run a service as a user. How do I run this as user? Another question, is it bad to use bash instead of sh for runit services? I guess if I use sh, then I could just put:

Code: [Select]
exec emacs --daemon

Do I have this all wrong? Thanks for any and all help!   :)

Re: Runit service for Emacs daemon

Reply #1
There are some examples here to get some inspiration:
http://smarden.org/runit/runscripts.html
Code: [Select]
# sudo -u (your user) (command)
works to run  a command as your user from a root shell. But if you exec that in a runscript, runit might try and restart sudo when it exits, so it will run over and over - but try it and see, with some harmless test command if you can think of one.
If it did, you could run that with no exec, then exec sleep infinity, there might be a better way to run a one shot service now but I forgot it  ;D

Off topic, does anyone get "leaking boot messages" with some  runit services still? Looking at those runscripts I wondered what this did at the start of some:
exec 2>&1
It redirects file descriptors which might be helpful.    

Re: Runit service for Emacs daemon

Reply #2
First of all, why would you torture yourself by using emacs? Use vi!!
All kidding aside, the proper flag if you want to use runit in combination of emacs would be --fg-daemon, not just --daemon since we want to keep it on the foreground.

The only problem is, it would run as root. I haven't figured out how to run a service as a user. How do I run this as user?
You can use chpst for that. So the proper command should be:
exec chpst -u <your username> emacs --fg-daemon

Another question, is it bad to use bash instead of sh for runit services?
It depends, if you have bash and plan to keep it installed, then no, since it's your own service, but #!/bin/sh is POSIX though and is guaranteed to exist in every single system. Ultimately, it's up to you.

Off topic, does anyone get "leaking boot messages" with some  runit services still? Looking at those runscripts I wondered what this did at the start of some:
exec 2>&1
It redirects file descriptors which might be helpful.    

You know, I wonder which services leak to the stdout (aside of NetworkManager). What if we do the reverse (1>&2), would it still leak? Hmm..
now only the dinit guy in artix

Re: Runit service for Emacs daemon

Reply #3
Thanks for your help. Now, I ran the runit emacs daemon with your

Code: [Select]
exec chpst -u <your username> emacs --fg-daemon

and I can see in htop that emacs --fg-daemon is running as my user, not as root. However, it seems emacs is not finding my emacs profile/preferences and is for sure not using root's preferences. So, I changed the runit script to be

Code: [Select]
exec chpst -u myusername emacs --fg-daemon -u myusername

to see if this would fix it, but I get this warning message in emacs:

Code: [Select]
Warning (initialization): Unable to create `user-emacs-directory' (~/.emacs.d/).
Any data that would normally be written there may be lost!
If you never want to see this message again,
customize the variable `user-emacs-directory-warning'.
Warning (initialization): An error occurred while loading ‘/home/myusername/.emacs’:

Symbol's function definition is void: ac-config-default

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with
the ‘--debug-init’ option to view a complete error backtrace.

Is this some kind of permissions issue (from using chpst) that I can fix? It also seems that now emacs thinks that ~/ is /, which I would assume is the main problem, but I am not sure.

Edit: I changed the script to this:

Code: [Select]
#!/bin/sh

export HOME=/home/myusername
# This makes sure that the emacs daemon runs as user myusername
exec chpst -u myusername emacs --fg-daemon -u myusername

and adding the $HOME variable loads my configuration, but emacs still defaults its path to /etc/runit/sv/emacs when going to load a file, and extensions like tramp (allowing one to edit files over ssh and with sudo) do not connect for some reason.

Re: Runit service for Emacs daemon

Reply #4
Before exec-ing the emacs daemon, try cd /home/myusername instead of export HOME=/home/myusername
now only the dinit guy in artix

Re: Runit service for Emacs daemon

Reply #5
Hmmm, this didn't change anything. Is there a way to actually make a user service running directory, that would run the runit service as a user, not just the runit service as my user? I've tried to look into this, but I could not figure it out on my own, as the official runit documentation is a little different from how runit works in artix.

Re: Runit service for Emacs daemon

Reply #6
It should work anywhere regardless of how it works. Since iirc the only differences between Artix and other runit implementations are stage1, stage3, and symlinks.

Anyway, as for emacs, as per https://hristos.lol/emacs-daemon-runit-service/, you need to set your $HOME and cd $HOME.
now only the dinit guy in artix

Re: Runit service for Emacs daemon

Reply #7
Thank you for that link. I am going to follow it and see if emacs will work with it.