Running compiled elixir app as a service(systemd)?

Hey all
I have a small elixir program compiled using Distillery 2.0 that I want to run whenever my computer boots up.
I figured it would be straight forward because it would just be running “./bin/app start” in a service, however I get the following error(s):

sudo journalctl -xe
>-- Subject: Unit zofi.service has finished start-up
>-- Defined-By: systemd
>-- Support: http://www.ubuntu.com/support
>-- 
>-- Unit zofi.service has finished starting up.
>-- 
>-- The start-up result is RESULT.
> Nov 28 15:51:51 jed-nuc7i3-04 zofi[900]: erlexec: HOME must be set
> Nov 28 15:51:51 jed-nuc7i3-04 zofi[900]: Unusable Erlang runtime system! This is likely due to being compiled for another system than the host is running
> Nov 28 15:51:51 jed-nuc7i3-04 systemd[1]: zofi.service: Main process exited, code=exited, status=1/FAILURE
> Nov 28 15:51:51 jed-nuc7i3-04 systemd[1]: zofi.service: Failed with result 'exit-code'.

-erlexec: HOME must be set
Not sure where to begin here since I haven’t seen references to ‘erlexec’ in the Distillery docs thus far.

-Unusable Erlang runtime system! This is likely due to being compiled for another system than the host is running

This is also a strange one because I’m deploying it on the same computer it was made.

Maybe I’m missing something obvious here. Any ideas?

Other Info:
OS: Ubuntu 18.04.1 LTS
Elixir 1.7.4 (compiled with Erlang/OTP 20)
Distillery version 2.0.12
rel/config.exs - Using defaults.
The service: https://pastebin.com/ZJHEikFt

How are you as a developer managing your erlang and elixir version? Are you using those provided by apt or are you using kiex, kerl, asdf, or something similar?

If the latter, make sure you use the apt installed version when building the release or create an environment using the tools which is usable by systemd.

I’m not sure about HOME and erlexec though…

Make sure you have set include_erts: true in your release config, this may fix the second warning.

erlexec is Erlang’s underlying binary, it starts the whole system up when you run erl. Distillery’s docs recommend using foreground instead of start here, I’d try that.

UPDATE: Just to elaborate a bit on erlexec, for posterity:

$ which erl
/usr/bin/erl

$ ls -l /usr/bin/erl
lrwxrwxrwx 1 root root 22 Sep 28 19:37 /usr/bin/erl -> ../lib/erlang//bin/erl*

$ cat /usr/lib/erlang/bin/erl
#!/bin/sh

<snip>

ROOTDIR="/usr/lib/erlang"
BINDIR=$ROOTDIR/erts-10.1/bin

<snip>

exec "$BINDIR/erlexec" ${1+"$@"}
# This is the actual executable file that starts the Erlang system up
$ file /usr/lib/erlang/erts-10.1/bin/erlexec
/usr/lib/erlang/erts-10.1/bin/erlexec: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=870fd3e2d6d8ace6d2c09044bd3545659c0691a0, stripped
1 Like

You should look at my PidFile - create and manage a PID file from the BEAM process thread, I have a systemd service description there that works well and I’ve been using for over a year now. You don’t need pid_file itself now as I think Distillery 2 baked in it’s functionality now, but check it’s docs to make sure (and to get the path to use). pid_file still works fine otherwise (I’m still using it). :slight_smile:

1 Like

Thanks everyone.
Turns out I just needed to run mix release with MIX_ENV=prod to compile to a proper production version with set_include_erts set to true. When previously I was just including the flag --env=prod, thinking it was sufficient.

2 Likes

Cool project. I might check this out if my little project grows into into something non-trivial :slightly_smiling_face:

1 Like

I did the same but still getting

/var/app/_build/prod/rel/afterglow/erts-10.3/bin/erlexec: 1: /var/app/_build/prod/rel/afterglow/erts-10.3/bin/erlexec: Syntax error: "(" unexpected
Unusable Erlang runtime system! This is likely due to being compiled for another system than the host is running
/var/app/_build/prod/rel/afterglow/erts-10.3/bin/erlexec: 1: /var/app/_build/prod/rel/afterglow/erts-10.3/bin/erlexec: Syntax error: "(" unexpected
Unusable Erlang runtime system! This is likely due to being compiled for another system than the host is running
/var/app/_build/prod/rel/afterglow/erts-10.3/bin/erlexec: 1: /var/app/_build/prod/rel/afterglow/erts-10.3/bin/erlexec: Syntax error: "(" unexpected
Unusable Erlang runtime system! This is likely due to being compiled for another system than the host is running