Skip to content

Instantly share code, notes, and snippets.

@arteezy
Last active May 9, 2024 22:22
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save arteezy/5d53d99f6ee617fae1f0db0576fdd418 to your computer and use it in GitHub Desktop.
Save arteezy/5d53d99f6ee617fae1f0db0576fdd418 to your computer and use it in GitHub Desktop.
Manage Puma with systemd and rbenv
[Unit]
Description=Puma Rails Server
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/app/current
ExecStart=/home/deploy/.rbenv/bin/rbenv exec bundle exec puma -C /home/deploy/app/shared/config/puma.rb
ExecStop=/home/deploy/.rbenv/bin/rbenv exec bundle exec pumactl -S /home/deploy/app/shared/tmp/pids/puma.state stop
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
@FinnWoelm
Copy link

Thanks a lot! 🎉

@DaveCollinsJr
Copy link

Fantastic. Thanks for taking the time to summarize!

@samholst
Copy link

You saved my life! After 5 hours of trying I found this! Thanks!

@ukazap
Copy link

ukazap commented Sep 7, 2018

is this only for one rails app?

@loop23
Copy link

loop23 commented Sep 12, 2018

@ukazap yes for the one in /home/deploy/app/current

@rromanchuk
Copy link

For whatever reason I had to use ExecStart=/bin/bash -lc 'bundle exec puma -C {deploy_dir}/shared/config/puma/production.rb' in my puma.service, otherwise i was getting the error below. Interestingly enough, i never noticed until i was tailing the logs because it was just immediately relaunching so i didn't even notice

Oct 24 08:33:37  puma[14012]: [14012] - Gracefully shutting down workers...
Oct 24 08:33:37 puma[14012]: On worker shutdown...
Oct 24 08:33:37 puma[14012]: On worker shutdown...
Oct 24 08:33:37  puma[18850]: Command stop sent success
Oct 24 08:33:37  puma[14012]: On worker shutdown...
Oct 24 08:33:37 puma[14012]: [14012] - Gracefully shutting down workers...
Oct 24 08:33:37puma[14012]: bundler: failed to load command: puma (/var/www/spar/releases/20181024082043Z/vendor/bundle/ruby/2.5.0/bin/puma)
Oct 24 08:33:37 ipuma[14012]: Errno::ECHILD: No child processes
Oct 24 08:33:37  puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:38:in `waitpid'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:38:in `block in stop_workers'
Oct 24 08:33:37 puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:38:in `each'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:38:in `stop_workers'
Oct 24 08:33:37 puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:391:in `block in setup_signals'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:515:in `rescue in run'
Oct 24 08:33:37puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cluster.rb:478:in `run'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/launcher.rb:184:in `run'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/shared/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/lib/puma/cli.rb:78:in `run'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/releases/20181024082043Z/vendor/bundle/ruby/2.5.0/gems/puma-3.12.0/bin/puma:10:in `<top (required)>'
Oct 24 08:33:37 i puma[14012]:   /var/www/spar/releases/20181024082043Z/vendor/bundle/ruby/2.5.0/bin/puma:23:in `load'
Oct 24 08:33:37  puma[14012]:   /var/www/spar/releases/20181024082043Z/vendor/bundle/ruby/2.5.0/bin/puma:23:in `<top (required)>'
Oct 24 08:33:37  systemd[1]: puma.service: Main process exited, code=exited, status=1/FAILURE
Oct 24 08:33:37  systemd[1]: puma.service: Failed with result 'exit-code'.
Oct 24 08:33:37 systemd[1]: Stopped puma.
-- Subject: Unit puma.service has finished shutting down

@rromanchuk
Copy link

actually i just figured it out, it was the ExecStop declaration that was causing it, once i removed that, i had proper restarts

@rromanchuk
Copy link

blah, because i'm using sockets. puma's own documentation is correct https://github.com/puma/puma/blob/master/docs/systemd.md

@dougcouvillion
Copy link

Thank you! This was a life saver.

@promisepreston
Copy link

promisepreston commented Dec 16, 2019

Thank you so much for this.
I made some modification to mine.

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

https://stackoverflow.com/a/59363127/10907864

@nirmal-vaja
Copy link

Hey I did the same but that prompt me to enter password I am not sure what the password is .... Can anyone help me on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment