Elixir apps as systemd services - info & wiki

Have you tried using GitHub - hauleth/erlang-systemd: systemd utilities for Erlang applications by @hauleth to help with the logs?

2 Likes

I’m just saving some more notes. While my notes above work for full production systems, the following is easier and works great for some internal microservices. Rather than build it, just run the mix phx.server. This way the logs are sent to systemd so you can follow them with: journalctl -u example -f

I also figured out how to connect to the iex CLI after the process is started with systemd. Warning: I can do this safely because my services are running behind a firewall. I’d read up on the --name “example@127.0.0.1” part of iex/elixir. You are exposing a management port/connection which is super helpful and a little more dangerous if someone naughty has access to that port. Enough talk, here is how I set it up:

sudo vi /etc/environment-example

PATH="/home/core/.asdf/shims:/home/core/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
SECRET_KEY_BASE=RlfalO4gogetyourownkeyzdNsLE
MIX_ENV=prod
DATABASE_URL=ecto://mydbuser:mypassword@192.168.1.257/example
PORT=5428

sudo vi /lib/systemd/system/example.service

[Unit]
Description=Example
After=network.target auditd.service

[Service]
EnvironmentFile=/etc/environment-example
User=core
Group=core
Type=simple
WorkingDirectory=/opt/example
ExecStart=/home/core/.asdf/shims/elixir --name "example@127.0.0.1" -S mix  phx.server
KillMode=process
TimeoutSec=0
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Here is how to connect to the iex/CLI of the running service:

mkdir ~/bin
vi ~/bin/example.sh

#!/bin/sh
exec iex --name console-example@127.0.0.1 --remsh example@127.0.0.1

chmod +x example.sh
~/bin/example.sh

3 Likes