How to get systemd to restart a Rails application with Puma

I struggled with this a week later and really can't find an answer. I deployed the Rails application with Capistrano. I use Puma as a server.

When I deploy, everything works fine. The problem is to start Puma on reboot and / or crash.

To get the deployment setup, I used this tutorial . I also use RVM. The problem, which, it seems to me, is to start the Puma service. Here is what I used (service file):

[Unit] Description=Puma HTTP Server After=network.target [Service] Type=simple #User=my-user WorkingDirectory=/home/my-user/apps/MyApp/current ExecStart=/home/my-user/apps/MyApp/current/sbin/puma -C /home/my-user/apps/MyApp/shared/puma.rb Restart=always [Install] WantedBy=multi-user.target 

This does not work. I started to think that the problem was that Ruby was not installed for all users, so I installed RVM for all users and still get the same problem. My server has only root and my-user.

After seeing how Capistrano is deployed, the command it runs is: cd /home/my-user/apps/MyApp/current && ( RACK_ENV=production /home/my-user/.rvm/bin/rvm default do bundle exec puma -C /home/my-user/apps/MyApp/shared/puma.rb --daemon ) . If I use the above command, I get a message from Systmd complaining about missing parameters. So I wrote a script with it and got a service file to call this script to start the application.

This does not work either. Please note that if I call the script from anywhere on the server, the script launches the application, so its a problem when setting up Systemd, but I can’t understand what is wrong, and I’m not sure how to debug it, I saw the debug page on the web -site System, but it did not help me. If I run systemctl status puma.service , all this tells me that the service is in a state of failure, but it does not tell me how and why.

Also worth noting: if I run bundle exec puma -C /home/my-user/apps/MyApp/shared/puma.rb from my application folder, it works fine, since I can duplicate this command using the Systemd service?

+8
linux ruby-on-rails nginx systemd puma
source share
2 answers

Have you viewed Foreman ? Foreman makes it easy to start and stop an application if it has multiple processes. By the way, it also provides an export function that can generate multiple systemd or upstart scripts for you to (restart) start and stop the application.

Since you are already using capistrano, you can use capistrano-foreman to fully integrate this with capistrano.

I hope you find some usefulness in these resources.

+2
source share

In the end, the problem was twofold: 1) rvm was not installed properly for all users, which meant that the deployment user did not have ruby ​​/ batch / etc available, and secondly, the script was also incorrect. For reference, here is a revised script that worked for me:

 [Unit] Description=Puma HTTP Server After=network.target [Service] Type=simple User=deployer WorkingDirectory=/var/www/apps/MRCbe/current ExecStart=/bin/bash -lc 'bundle exec puma -C /var/www/apps/MRCbe/shared/puma.rb' Restart=always [Install] WantedBy=multi-user.target 
+17
source share

All Articles