krasenyp

krasenyp

A simple rc.d script to run your release as a daemon on FreeBSD

I don’t know if anyone is running Elixir releases as system services on FreeBSD but below is the simplest working solution I’ve found and been using so far.

The example service is called repp , and the following script should be placed in /usr/local/etc/rc.d/ .

# PROVIDE: repp
# REQUIRE: epmd
# AFTER: epmd
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable Repp
#
# repp_enable="YES"

. /etc/rc.subr

name="repp"
rcvar="repp_enable"

load_rc_config $name

: ${repp_enable:="NO"}
: ${repp_user:="www"}

repp_command="/usr/local/lib/repp/bin/repp"

cpidfile="/var/run/${name}/${name}.pid"
pidfile="/var/run/${name}/${name}d.pid"
command=/usr/sbin/daemon
command_args="-P ${pidfile} -p ${cpidfile} -r -f -S ${repp_command} start"

start_precmd="${name}_prestart"

repp_prestart()
{
    if [ ! -d /var/run/${name} ]; then
        install -d -o ${repp_user} /var/run/${name}
    fi
}

run_rc_command "$1"
  • The service is network-facing and that’s why I use the www user.
    • I never run services as root, even in jails.
  • FreeBSD’s daemon utility is used to detach the process from the terminal.
    • The -r flag makes daemon restart its child, the beam.smp process, when it crashes.
    • -f redirects stdout and stderr to /dev/null before -S redirects them to syslog .
  • The canonical place to create pid files is /var/run so the directory /var/run/repp is created if it doesn’t exist already.

From here, you can make it as complex as you want.

A similar setup is covered in Practical rc.d scripting in BSD | FreeBSD Documentation Portal. If anyone is interested in the topic, I’d advise them to look in the official docs first. I read many articles which claimed to show working solutions but they either didn’t use the daemon utility and didn’t redirect logs to syslog or used the daemon utility in conjunction with <release> daemon which creates an OS supervisor, which starts run_erl, which starts the release. Not optimal.

Where Next?

Popular in Guides/Tuts Top

anuragg
Hi everyone! I’m the founder of Render, a new cloud provider with native support for Elixir. When we launched Elixir support the most po...
New
marcelo
I wrote a small article on how to use current_user with coherence on Phoenix. There are already a couple of blogposts about it but I thin...
New
dgamidov
Hi folks, Just a short instruction. Maybe it will help somebody. https://v4-alpha.getbootstrap.com/getting-started Install boostrap...
New
OndrejValenta
Me and my boys started a new website specifically designed for other ASP.NET programmers that struggle, as we do, with their transformati...
New
annad
I’m posting this for developers who are totally new to Phoenix like myself. This is all probably obvious to more skilled Phoenix develope...
New
AstonJ
This was originally posted on my blog, but since my I’ve taken it down (I hadn’t posted anything on it for a while and since it was runni...
New
georgeguimaraes
Another cool plugin for Neovim, GitHub - jmbuhr/otter.nvim: Just ask an otter! 🦦 makes it possible to run linters for embedded code, like...
New
f0rest8
Hi, TLDR: form attribute set on the input fields and button submit. I just wanted to share a solution I discovered when making live inl...
New
lukertty
Install web-mode and mmm-mode first and put this in your config file: (require 'mmm-mode) (require 'web-mode) (setq mmm-global-mode 'may...
New
AstonJ
This blog post hit my timeline earlier, and I’ve also been learning about some fantastic Elixir related tips via @pragdave’s new online c...
New

Other popular topics Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement