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

bluegene
Hi guys, I’ve been on a personal journey to learn Elixir for the past two years. During this journey I’ve been using the spaced repetiti...
New
voltone
The EEF’s Security WG has released the first public draft of the Secure Coding and Deployment Hardening Guidelines for BEAM languages. “...
New
niku
I write an article Parameterized testing with ExUnit.The key concept is using ExUnit.Case.register_test/4 such as ExUnit.start() defmod...
New
mudasobwa
The post covering how to generate nifty types to use in @spec in compile time with macros.
New
jshprentz
Geoffrey Lessel’s 2019 book, Phoenix in Action, was written for Phoenix 1.4. I found that the book’s code examples did not match the cur...
New
slouchpie
Warmest greetings, comrades. I recently started using :dns_cluster (GitHub - phoenixframework/dns_cluster: Simple DNS clustering for dis...
New
jtormey
Hello! Having written a lot of LiveView code, I’ve made some VS Code snippets to speed up writing callbacks for LiveViews and LiveCompon...
New
pel_daniel
Hi! I created some animations to explain what happens internally with map &amp; reduce functions. The code is open source. Any feedback...
New
caspg
Hi everyone, I recently implemented a real-time search feature in a Phoenix application using LiveView and Tailwind, and I wanted to sha...
New
smpallen99
Some advice for Elixir programmers. I was reviewing someone’s Elixir Code yesterday and found a deadlock condition bug in a GenServer im...
New

Other popular topics Top

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 48475 226
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement