Automatically Restart Elixir Applications after Server Reboot with Edeliver, Ubuntu, and Systemd

Edeliver is an awesome deployment tool for Elixir applications, including Phoenix apps, but one thing it lacks (intentionally) is support for auto-restarting your Elixir app after server reboots.

Out of the box, if you want to restart your app after a server reboot, you can just run

$ mix edeliver restart production

in a terminal from your local app directory.

Having to run this command after every reboot is a minor annoyance, but, what if your hosting company decides to restart your server in the middle of the night? What if you have many elixir apps running on the same server? What if you’re just OCD about never wanting to type commands again and just want the damn thing automated? Well, one way to solve that requires a systemd service.

Starting and Stopping Your App from the Server Itself

Edeliver uses another great tool, Distillery, to help build releases, and Distillery provides several useful command line tasks, including tasks that start and stop your app. This is what we’ll use in our systemd script.

NOTE: This article assumes you have setup your deployment process the same way as this article.


Step 1: Create a Systemd Unit File

We need to create a .service file that will run on restart in systemd’s system directory. Since we’re on Ubuntu 16.04, the directory we’re looking for is at /etc/systemd/system, but depending on your OS it might be /usr/lib/systemd/system or /lib/systemd/system. Regardless, you now need to create a file in that directory called your_app.service with the following contents:

# /etc/systemd/system/your_app.service
[Unit]
Description=Your App
After=network.target

[Service]
User=appuser
Group=appuser
Restart=on-failure
Environment=HOME=/home/appuser/app_release/your_app
ExecStart=/home/appuser/app_release/your_app/bin/your_app foreground
ExecStop=/home/appuser/app_release/your_app/bin/your_app stop

[Install]
WantedBy=multi-user.target
Step 2: Check the syntax

After you’ve saved the unit file, verify its syntax with the command:

$ sudo systemd-analyze verify /etc/systemd/system/your_app.service

If all is well, the command returns nothing, otherwise it will tell you where it encountered an error.

Step 3: Enable the service

You can enable and start your new service with the following commands:

$ sudo systemctl enable your_app
$ sudo systemctl start your_app

And that’s it! If you do a server reboot now, your app should automatically be back up and running!

2 Replies to “Automatically Restart Elixir Applications after Server Reboot with Edeliver, Ubuntu, and Systemd”

  1. Thanks for this nice tutorial!

    It works as intended, however, when I started my app with ‘systemctl start’ it does not write to the log in ‘/home/user/app/var/log/erlang.log’. When started from local machine with ‘mix edeliver start’ it writes to this log as usual.

    Do you know how can I fix this?

    1. Thanks for the feedback, Stas! I had not noticed this behavior. I implemented this because my hosting company has had unplanned server restarts in the past. It works fine for that use case, but I’ve also experienced some issues trying to push upgrades using edeliver when the app has been started in this way.

      I’ll definitely look into this when I get the chance, and I’ll let you know when I update the article. If you figure it out in the meantime though, I’d love to hear about it.

Leave a Reply

Your email address will not be published. Required fields are marked *