How to start rails or puma (installed with rvm) with systemd

Just a few notes I took about how to start rails server or puma (installed with rvm) with systemd.

I started reading this question on StackOverflow, but it has no answers. Then I found out this gist containing an interesting example: it shows how to create a wrapper with rvm, and then how to use this wrapper to start the server.

I read more in systemd.service documentation and the rails command line documentation and wrote this (gist):

# based on https://gist.github.com/twtw/5494223
# create systemd service file for rails/puma startup
# 0. [if required: rvm use ruby@default]
# 1. rvm wrapper default systemd rails
# 2. put this file in /etc/systemd/system/rails-puma.service
# 3. systemctl enable rails-puma
# 4. systemctl start rails-puma
[Unit]
Description=Rails-Puma Webserver
 
[Service]
Type=simple
User=app
WorkingDirectory=/home/app/your-app
ExecStart=/home/app/.rvm/bin/systemd_rails server -e production
TimeoutSec=15
Restart=always
 
[Install]
WantedBy=multi-user.target

I didn’t get to test it yet, if it works for you let me know :)

Advertisements