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

kuon
I have a page with a large state that is loaded from DB, let’s call that “data”. I have a root live view that load “data” on mount and r...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
hauleth
Some time ago someone suggested me to write article about how I have configured my Vim to work with Elixir, now there it is: https://med...
New
kokolegorille
Hello dear alchemists, There was this question some days ago here about the deployment to a VPS. As I was in the process of deploying t...
New
OvermindDL1
Ran across this recently, it's a set of cheatsheet inforgraphic things of the OTP behaviours on the BEAM:
New
njwest
In the process of developing a Phx-based multiplayer experience, I found myself with so many browser tabs open with Elixir gaming resourc...
New
berts-4865
Here is a quick guide to uploading a file from the browser to DO spaces. It is crude, but will hopefully save sometime time and frustrat...
New
jswny
Hello everyone, I recently redesigned my entire deployment process for Phoenix apps based on Docker. I really like the strategy that I ca...
New
fmcgeough
pipe into case? I use that fairly frequently…unless I’m misunderstanding what you’re wanting…could be… its still very early… str = "Hell...
New
tonydang
I recently got inertia-phoenix (an Inertia.js adapter for Phoenix) working with Svelte. Setting up the server-side rendering (SSR) with S...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement