Re: How do you create service scripts for erlang applications?
Date: Tue, 10 Mar 2026 21:35:52 UTC
Caveat that I’m building and running Elixir applications, but I use the ex_freebsd hex package. Even if you don’t use it directly, there may be some things in the templates there that would be useful to you.
Best,
Sax
> On Mar 10, 2026, at 2:30 PM, Todd Greenwood <toddg@zwrob.com> wrote:
>
> Hi Freebsd-Erlangers. This is my first post here...
>
> Question: In FreeBSD, how do you write service scripts for BEAM applications? Is there a template I can follow?
>
> I have simple needs. My app currently logs to stdout. I want to run the app as a daemon and send the logs to a file. Later, I'll get sophisticated and use more of Erlangs tooling for logs and log rotation, etc. And after that I'll figure out how to use syslog and send all my logs to a log server.
>
> Here's what I've started with:
>
> ```
> #!/bin/sh
> #
> # PROVIDE: gws
> # REQUIRE: DAEMON
>
> PACKAGE=myapp
> BASE=/opt/myapp
>
> . /etc/rc.subr
>
> name="myapp"
> desc="dummy app"
> rcvar="${name}_enable"
> command="/usr/local/lib/erlang28/bin/erl"
> command_args="-pa \"$BASE\"/*/ebin -eval \"$PACKAGE@@main:run($PACKAGE)\" -noshell"
> load_rc_config "${name}"
>
> set_rcvar
> pidfile="/var/run/${name}.pid"
> procname="${name}"
>
> run_rc_command "$1"
> ```
>
> This is not great b/c:
> * the service does not run as a daemon
> * logs go to stdout and not to a file
>
> I looked at this blog entry:
> https://www.mo4tech.com/a-standalone-way-to-launch-an-erlang-application-daemon.html
>
> and it suggested using the 'detached' flag, like this:
>
> ```
> command_args="-pa \"$BASE\"/*/ebin -eval \"$PACKAGE@@main:run($PACKAGE)\" -detached"
> ```
>
> That's better, b/c now the erlang aplication is backgrounded.... However, the output is not going to a log, and the 'service stop' and 'service restart' commands don't do anything.
>
> Any suggestions welcome.
>
> -ToddG
>