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.