Setting up an elixir app to run under systemd

December 12, 2021

Define a service file

Defining the following ensures that even after system restart your service is restarted

# /lib/systemd/system/torrent_top_10.service
Description=Run top 10 torrent service
# Wait until the network is available

# Run the binary that was generated by mix release
ExecStart=/home/pi/redacted-discord-bot/redacted-discord-bot-main/_build/prod/rel/app/bin/app start


Create a mix release

# Before doing this make sure your environment has the necessary variables that are accessible. I recommend using direnv to manage this
MIX_ENV=prod mix release

Tell systemd about the new changes

# Reload the daemon (pronounce it with me de-mon) so it picks up on the .service file
sudo systemctl daemon-reload

# Start your new service
sudo systemctl start torrent_top_10

# Check the status of the service to make sure it's healthy
sudo systemctl status torrent_top_10
# Here's some example output
● torrent_top_10.service - Run top 10 torrent service
   Loaded: loaded (/lib/systemd/system/torrent_top_10.service; e
   Active: active (running) since Sun 2021-12-12 18:31:44 MST; 5
 Main PID: 18293 (beam.smp)
    Tasks: 27 (limit: 4915)
   CGroup: /system.slice/torrent_top_10.service
           ├─18293 /home/pi/redacted-discord-bot/redacted-discor
           ├─18334 erl_child_setup 1024
           ├─18370 inet_gethost 4
           └─18371 inet_gethost 4


I'm curious to see how the daemon option would work and if that would be better than passing start to the mix release.

# Check usage from the app
Usage: app COMMAND [ARGS]

The known commands are:

    start          Starts the system
    start_iex      Starts the system with IEx attached
    daemon         Starts the system as a daemon
    daemon_iex     Starts the system as a daemon with IEx attached
    eval "EXPR"    Executes the given expression on a new, non-booted system
    rpc "EXPR"     Executes the given expression remotely on the running system
    remote         Connects to the running system via a remote shell
    restart        Restarts the running system via a remote command
    stop           Stops the running system via a remote command
    pid            Prints the operating system PID of the running system via a remote command
    version        Prints the release name and version to be booted

© 2023, Built with ❤️ by Blake Dietz