How to get systemd to restart Rails App with Puma
Have you looked into Foreman ?
Foreman makes it easy to start and stop your application if it has multiple processes.
Incidentally it also provides an export function that can generate some systemd
or upstart
scripts for you to (re)start and stop your application.
As you are already using capistrano you can use capistrano-foreman to integrate all this nicely with capistrano.
I hope you find some use in these resources
How to get a Rails App Deployed with Puma and Capistrano to start on reboot
I have a similar service but I declared slightly different /etc/systemd/system/puma.service
[Unit]
Description=Puma Control
After=network.target auditd.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/etc/puma/puma-start
ExecStop=/etc/puma/puma-stop
[Install]
WantedBy=multi-user.target
then in /etc/puma/puma-start
#!/bin/bash -
cd /home/changeuser/apps/changeapp/current && ( export RACK_ENV="production" ; /home/changeuser/.rvm/bin/rvm default do bundle exec puma -C /home/changeuser/apps/changeapp/shared/puma.rb --daemon )
and in /etc/puma/puma-stop
#!/bin/bash -
cd /home/changeuser/apps/changeapp/current && ( export RACK_ENV="production" ; /home/changeuser/.rvm/bin/rvm default do bundle exec pumactl -S /home/changeuser/apps/changeapp/shared/tmp/pids/puma.state stop )
remember after setting up to execute
chmod +x /etc/puma/puma-start
chmod +x /etc/puma/puma-stop
systemctl enable puma
and then to test
systemctl start puma
systemctl stop puma
systemctl restart puma
systemctl status puma
I am unable to start puma.service using systemd
Ok. Fixed the issue. It was indeed caused by rubygems-bundler
.
Uninstall rubygems-bundler
with
gem uninstall rubygems-bundler
My exact issue was not running following command:
executable-hooks-uninstaller
Everything works now.
`
puma systemd script doesn't start puma
Here's the solution:
[Unit]
Description=Test Puma HTTP Server
After=network.target
[Service]
Type=simple
User=rails
WorkingDirectory=/var/www/test-optimum/current
ExecStart=/bin/bash -lc 'RAILS_ENV=production bundle exec puma -C /var/www/test-optimum/current/config/puma.rb'
ExecStop=/home/rails/.rvm/bin/rvm default do bundle exec pumactl -S /var/www/test-optimum/shared/tmp/pids/puma.pid stop
PIDFile=/var/www/test-optimum/shared/tmp/pids/puma.pid
Restart=always
[Install]
WantedBy=multi-user.target
Puma Systemd Configuration for Rails Not Working
Here's how I fixed it:
Install Puma if it is not already installed.
Run the command
which puma
to print the installation directory of the puma server on your machine, which is usually installed in/home/your-username/.rbenv/shims/puma
directory.Open the
puma.service
file located at/etc/systemd/system/puma.service
by the running the command below:sudo nano /etc/systemd/system/puma.service
Copy the Puma service configuration file below into it and save.
Puma Service Configuration
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=deploy
# The path to the your application code root directory
WorkingDirectory=/home/deploy/my-app
# The command to start Puma
ExecStart=/home/deploy/.rbenv/shims/puma -C /home/deploy/my-app/config/puma.rb
# The command to stop Puma
ExecStop=/home/deploy/.rbenv/shims/puma -S /home/deploy/my-app/config.puma.rb
# Path to PID file so that systemd knows which is the master process
PIDFile=/home/deploy/my-app/tmp/pids/puma.pid
# Should systemd restart puma?
# Use "no" (the default) to ensure no interference when using
# stop/start/restart via `pumactl`. The "on-failure" setting might
# work better for this purpose, but you must test it.
# Use "always" if only `systemctl` is used for start/stop/restart, and
# reconsider if you actually need the forking config.
Restart=always
[Install]
WantedBy=multi-user.target
Note:
- For
ExecStart
: ExecStart=/your-puma-directory-path -C /your-app-puma-config-file-path - For
ExecStop
: ExecStop=/your-puma-directory-path -S /your-app-puma-config-file-path - There is no need to define
ExecReload
orExecRestart
, they work out of the box.
You can now start the puma server using the command:
sudo systemctl start puma
OR restart the puma server using the command:
sudo systemctl restart puma
OR check the status puma server using the command:
sudo systemctl status puma
OR stop the puma server using the command:
sudo systemctl stop puma
That's all.
I hope this helps
Systemd and Puma in an infinite startup loop
You should remove --daemon
option from the systemd, there are several types of unit:
Configures the unit process startup type that affects the
functionality of ExecStart and related options. One of:simple – The
default value. The process started with ExecStart is the main process
of the service.forking – The process started with ExecStart spawns a
child process that becomes the main process of the service. The parent
process exits when the startup is complete.oneshot – This type is
similar to simple, but the process exits before starting consequent
units. dbus – This type is similar to simple, but consequent units are
started only after the main process gains a D-Bus name.notify – This
type is similar to simple, but consequent units are started only after
a notification message is sent via the sd_notify() function.idle –
similar to simple, the actual execution of the service binary is
delayed until all jobs are finished, which avoids mixing the status
output with shell output of services.
The default value is simple, for the sake of puma configuration you use --daemon
options which contradict with the systemd configuration.
Related Topics
Checking If Activerecord Find Returns a Result
When to Use Self in Module's Methods
Can't Install Therubyracer in Jruby
Fastest Way to Find a String into an Array of String
How to Make a Custom Environment in Rails a Default Environment
Coping with "String Contains Null Byte" Sent from Users
Run Selenium with Chrome Driver on Heroku: 'Cannot Find Chrome Binary'
How to Add Attribute to Nokogiri Node
Is There a Definitive Reference Document for Ruby Syntax
Combine Thumbnails to One Large Image with Rmagick
Ruby - Problems with Expect and Pty
How to Scroll a Web Page Using Watir
How to Redirect Back to a Page I'M Currently On
Using Negative Conditions Within Regular Expressions
Error Connecting to Redis on 127.0.0.1:6379 (Errno::Econnrefused) - Wercker