Automatically Restart Elixir Applications after Server Reboot with Edeliver/Distillery, Ubuntu, and Crontab

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 is to use a crontab job.

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: Open the Crontab Edit File

Ok, if you’ve followed the setup linked above, you have a dedicated user on your production server just for your app. SSH into that server as that user and run the following command:

$ crontab -e

If this is your first time running that command, it will ask you what editor you want to use. Let’s go with nano.

Step 2: Add a Reboot Job to The Crontab File

Next you want to make use of crontab’s special @reboot string and use the Distillery start command to start up your app. Add this line to the bottom of the file:

@reboot /home/youruser/app_release/your_app/bin/your_app start

Obviously you’re going to want to replace the stuff in orange with your own info. One you’ve added the line, save and close the file by hitting Ctrl+x then Ctrl+y then Enter.

Step 3: Test It Out

Now do a sudo reboot and your app should automatically be back up and running in a matter of seconds.

3 Replies to “Automatically Restart Elixir Applications after Server Reboot with Edeliver/Distillery, Ubuntu, and Crontab”

  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.

    2. I updated this article to use Crontab instead of Systemd, and it basically solved all the problems we were running into.

Leave a Reply

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