Unable to run Puma as daemon OptionParser::AmbiguousOption: ambiguous option: -d - ruby-on-rails

I upgraded to Puma 5.0.2 and started my rails app as usual with:
bundle exec puma -d -e production -b unix:///home/user/app/tmp/puma.sock
Now I get the error:
OptionParser::AmbiguousOption: ambiguous option: -d
What is the proper way to run puma as a daemon?

Context: Quick links:
Daemonize option has been removed without replacement as of Puma 5.0.0 (Source: https://github.com/puma/puma/blob/master/History.md)
You may refer this section for daemonization in their documentation: https://github.com/puma/puma/blob/master/docs/deployment.md#should-i-daemonize
Solution:
Create a systemd service for puma depending on your OS distro.
Configure your environment in config/puma in your app directory.
Add a service file named puma.service in /etc/systemd/system (the path works for me on SLES15).
Here is a sample that works for me (replace text within <> as per your needs):
[Unit]
Description=Puma HTTP Server
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
User=<UserForPuma>
WorkingDirectory=<YourAppDir>
Environment=RAILS_MASTER_KEY=<EncryptionKeyIfUsedByRailsApp>
ExecStart=/usr/bin/rails s puma -b 'ssl://127.0.0.1:3000?key=<path_to_privatekey.key>&cert=<path_to_certificate.crt>' -e production
Restart=always
RestartSec=2
KillMode=process
[Install]
WantedBy=multi-user.target
Save the above content as a file named puma.service in the directory path mentioned above.
After this just enable and start the service:
# systemctl daemon-reload
# systemctl --now enable puma.service
Created symlink /etc/systemd/system/multi-user.target.wants/puma.service → /etc/systemd/system/puma.service.
# systemctl status puma
● puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2020-10-09 12:59:28 CEST; 7s ago
Main PID: 2854 (ruby.ruby2.5)
Tasks: 21
CGroup: /system.slice/puma.service
├─2854 puma 5.0.2 (ssl://127.0.0.1:3000?key=<your_key_path.key>&cert=<your_cert_path.crt>) [rails-app-dir]
├─2865 puma: cluster worker 0: 2854 [rails-app-dir]
└─2871 puma: cluster worker 1: 2854 [rails-app-dir]
Check puma status:
ps -ef | grep puma
This should now show running puma processes (main process and worker processes).
Here is a link for beginners on how to create a systemd service:
https://medium.com/#benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6
Systemd docs:
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://www.freedesktop.org/software/systemd/man/systemd.service.html
Sorry but I am not a Windows person, but I believe that the idea is same. Anyone working in Windows may try creating a bat file and running it in the background as a Windows service.
Hope that helps.

This gem on Github looks like a good source to start from:
https://github.com/kigster/puma-daemon

Related

puma:restart cap production deploy service not found

Rails 7.0.4
Puma 6.0
I am so close to getting cap production deploy to work in production but I'm stuck on puma:restart
I have an active service on the server
deploy#reports:~/apps/R7-reporting-2022/current$ sudo systemctl status R7-reporting-2022_puma_production.service
[sudo] password for deploy:
● R7-reporting-2022_puma_production.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/R7-reporting-2022_puma_production.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2022-11-06 19:24:32 UTC; 1h 3min ago
Main PID: 612 (puma)
Tasks: 10 (limit: 4575)
Memory: 177.7M
CPU: 2.258s
CGroup: /system.slice/R7-reporting-2022_puma_production.service
└─612 "puma 6.0.0 (unix:///home/deploy/apps/R7-reporting-2022/shared/tmp/sockets/R7-reporting-2022-puma.sock)" "" "" "" "" "" ">
Nov 06 19:24:32 reports systemd[1]: Started Puma HTTP Server.
When I run bundle exec cap production deploy I get the following error
00:24 puma:restart
01 /bin/systemctl --user restart R7-reporting-2022_puma_production
01 Failed to restart R7-reporting-2022_puma_production.service: Unit R7-reporting-2022_puma_production.service not found.
I'm wondering if the --user part of the command is what's causing the issue?
after further testing I can run the command without --user flag on the server.
/usr/bin/env /bin/systemctl restart R7-reporting-2022_puma_production
Multiple identities can be used for authentication:
1. user (user)
2. deploy user,,, (deploy)
Choose identity to authenticate as (1-2): 2
Password:
==== AUTHENTICATION COMPLETE ===```
I switched from puma to passenger using the following guide. Great instructions, simple implementation. Props to the writer.
Rails Deploy on Ubuntu using Capistrano

I am unable to start puma.service using systemd

It has been running well until yesterday. Now, suddenly I cannot get puma running at all. I am currenly using tmux to run puma and run my app. But, it fails when I try to run server using systemctl. Note:- I updated rake and other gems then had to revert back, which gave me error. That I had activated new version of rake, and using older one. So, I decided to install gem 'rubygems-bundler'. Could this have been the cause of the issue?. I have removed this gem now. But, it still doesn't work.
Puma Version: 3.12.0
Here's the puma.service status:
puma.service - Puma HTTP Server
Loaded: loaded (/etc/systemd/system/puma.service; enabled; vendor preset: enabled)
Active: failed (Result: start-limit-hit) since Wed 2020-07-01 12:06:47 UTC; 1s ago
Process: 14640 ExecStart=/home/deploy/.rbenv/shims/puma -C config/puma.rb -p 9100 -e staging (code=exited, status=1/FAILURE)
Main PID: 14640 (code=exited, status=1/FAILURE)
systemd[1]: puma.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: puma.service: Unit entered failed state.
systemd[1]: puma.service: Failed with result 'exit-code'.
systemd[1]: puma.service: Service hold-off time over, scheduling restart.
systemd[1]: Stopped Puma HTTP Server.
systemd[1]: puma.service: Start request repeated too quickly.
systemd[1]: Failed to start Puma HTTP Server.
systemd[1]: puma.service: Unit entered failed state.
systemd[1]: puma.service: Failed with result 'start-limit-hit'.
My pumar.rb file is:
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count
bind "unix:///tmp/production-puma.sock"
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
ActiveRecord::Base.establish_connection
end
app_path = File.expand_path(File.dirname(__FILE__) + '/../')
stdout_redirect "/#{app_path}/log/puma.stdout.log", "/#{app_path}/log/puma.stderr.log"
Here is puma.service file
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
Requires=puma.socket
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
#Type=forking
# Preferably configure a non-privileged user
User=deploy
# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.
WorkingDirectory=/home/deploy/app
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
ExecStart=/home/deploy/.rbenv/shims/puma -C config/puma.rb -p 9100 -e staging
Restart=always
[Install]
WantedBy=multi-user.target
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.
`

systemd init script for resque worker

i habe an nginx webserver running on an Ubuntu 16.04 Server.
Now i am trying to build an init script for resque worker and scheduler for a Rails app.
I created a file resque-worker.service in "/etc/systemd/system/" and it looks like this:
[Unit]
Description=resque-worker for pageflow
[Service]
Type=forking
ExecStart=/home/pageflow/pageflow_daad/rake resque:scheduler QUEUE=* RAILS_ENV=production > /home/pageflow/pageflow_daad/log/resqueschedule.log &
[Install]
WantedBy=multi-user.target
For some reason after executing "systemctl daemon-reload" and "systemctl start name.service" i get this error:
$ systemctl status resque-worker.service
● resque-worker.service - resque-worker for pageflow
Loaded: loaded (/etc/systemd/system/resque-worker.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2017-05-17 15:52:38 CEST; 15s ago
Process: 28096 ExecStart=/home/pageflow/pageflow_daad/rake resque:scheduler QUEUE=* RAILS_ENV=production > /home/pageflow/pageflow_daad/log/resqueschedule.log & (code=exited, status=203/EXEC)
May 17 15:52:38 ostheim systemd[1]: Starting resque-worker for pageflow...
May 17 15:52:38 ostheim systemd[1]: resque-worker.service: Control process exited, code=exited status=203
May 17 15:52:38 ostheim systemd[1]: Failed to start resque-worker for pageflow.
May 17 15:52:38 ostheim systemd[1]: resque-worker.service: Unit entered failed state.
May 17 15:52:38 ostheim systemd[1]: resque-worker.service: Failed with result 'exit-code'.
In this case i used the root path of my Rails app for "/home/pageflow/pageflow_daad/rake".
The times before where i tried the path of the rake binary i got the error:May 17 15:30:26 ostheim rake[26846]: rake aborted!
May 17 15:30:26 ostheim rake[26846]: ArgumentError: couldn't find HOME environment -- expanding~'`
I hope someone with more experience in this can help me out.
Thanks in advance and best regards,
Ronald
After studying the several docu sites and the documentation itself i found a way to get this running. Just wanted to post this if someone finds this to be helpfull:
[Unit]
Description=resque-scheduler for pageflow
[Service]
User=yourUser
WorkingDirectory=/path/to/rails/app
ExecStart=/path/to/executeable/rake resque:scheduler &
Environment=QUEUE=*
Environment=RAILS_ENV=production
[Install]
WantedBy=multi-user.target
With this script, a
systemctl daemon-reload
&
systemctl start example.service
The Service startet running and runs like a charm.
I think there 2 types of errors inside the systemd unit. Here are some advices :
1) Let systemd capture your application logs, then use journalctl to look at it
journalctl -u resque-worker.service
2) Use env the systemd way :
[Unit]
Description=resque-worker for pageflow
[Service]
Type=forking
ExecStart=/home/pageflow/pageflow_daad/rake resque:scheduler
Environement=QUEUE=*
Enrironement=RAILS_ENV=production
[Install]
WantedBy=multi-user.target
Then,
systemctl daemon-reload
systemctl restart resque-worker.service

unicorn failing to start on ubuntu 15.04

I'm following an installation tutorial for unicorn and nginx using this tutorial on ubuntu 15.04. Everything works until the section "Create Unicorn Init Script" where I create the init script, change the required variables, change the permissions and then run
sudo service unicorn_my_app start
Where I get an error that reads
Job for unicorn_my_app.service failed. See "systemctl status unicorn_my_app.service" and "journalctl -xe" for details
When I run
systemctl status unicorn_my_app.service
The output is:
● unicorn_my_app.service - LSB: starts the unicorn app server
Loaded: loaded (/etc/init.d/unicorn_my_app)
Active: failed (Result: exit-code) since Sun 2015-09-27 23:22:49 PDT; 1min 19s ago
Docs: man:systemd-sysv-generator(8)
Process: 26576 ExecStart=/etc/init.d/unicorn_my_app start (code=exited, status=127)
What is happening? why won't unicorn start?

run puma server as a service at centos 7 - no ruby found

There are many things I do not understand, so my question may be silly.
I want to run a puma ror server as a systemd service at centos 7. Use ruby installed using rvm.
My puma_test.service file is:
[Unit]
Description=Puma application server
After=network.target
[Service]
WorkingDirectory=/var/www/test_app
Environment=RAILS_ENV=development
PIDFile=/var/www/shared/pids/puma.pid
ExecStart=/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.9.4/bin/bundle exec puma -e development -b unix:///var/www/shared/pids/puma.sock --pidfile /var/www/shared/pids/puma.pid
[Install]
WantedBy=multi-user.target
but when I run it, it does not work. I get error (from journalctl):
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: Starting Puma application server...
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: Started Puma application server.
kwi 18 22:56:15 vps150852.ovh.net bundle[2072]: /usr/bin/env: ruby: No such file or directory
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: puma_test.service: main process exited, code=exited, status=127/n/a
kwi 18 22:56:15 vps150852.ovh.net systemd[1]: Unit puma_test.service entered failed state.
when I run i /usr/www/test_app
/usr/local/rvm/gems/ruby-2.2.1/gems/bundler-1.9.4/bin/bundle exec puma -e development -b unix:///var/www/shared/pids/puma.sock --pidfile /var/www/shared/pids/puma.pid
everything works fine, but I am probably doing something wrong
Looks like you need to load rvm when you run your task. systemd run in shell, not in bash, your bashrc will not be loaded

Resources