'Deploy via capistrano fail: "sudo: /bin/systemctl: command not found"

Now I'm transferring my environment following the below.

Environment PreviousVersion NewVersion
Rails 5.1.2 7.0.0
Ruby 2.4.1 3.0.0
capistrano 3.10.1 3.16.0
capistrano3-puma 3.1.1 5.2.0
Production environment Amazon EC2 Linux Amazon EC2 Linux

When deploy via capistrano, I got an error: sudo: /bin/systemctl: command not found.
As a test, I typed systemctl in production enviroment, got -bash: systemctl: command not found.

I heard that systemd became the default from capistrano3-puma 5.x.
I suspect it's related.
Because before transferring, that kind of error was nothing.

So.. I had no choice but to use bundle exec pumactl start and service nginx restart in production environment.
Update was success.

But I really want to deploy via capistrano for save my time.
How can I do?

puma.rb

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count

port        ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "development" }
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
plugin :tmp_restart

production.rb

set :stage, :production

role :app, %w[deploy@xxx]
role :web, %w[deploy@xxx]
role :db, %w[deploy@xxx]

server 'xxx', user: 'deploy', roles: %w[web app]

set :sitemap_roles, :web

set :whenever_roles, -> { :web }
set :whenever_options, -> { { roles: fetch(:whenever_roles) } }
set :whenever_identifier, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
set :whenever_environment, -> { fetch :rails_env, 'production' }
set :whenever_variables, -> { "environment=#{fetch :whenever_environment}" }
set :whenever_update_flags,
    -> {
      "--update-crontab #{fetch :whenever_identifier} --set #{fetch :whenever_variables}"
    }
set :whenever_clear_flags,
    -> { "--clear-crontab #{fetch :whenever_identifier}" }

set :ssh_options,
    {
      keys: %w[/Users/xxx/.ssh/id_rsa],
      forward_agent: true,
      auth_methods: %w[publickey],
    }

deploy.rb

set :application, 'xxx'
set :repo_url, '[email protected]:xxx'


set :deploy_to, '/var/www/xxx'

set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :stage, :staging
set :deploy_via, :remote_cache
set :deploy_to, "/var/www/#{fetch(:application)}"
set :puma_bind,
    "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.access.log"
set :puma_error_log, "#{release_path}/log/puma.error.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_restart_command, 'bundle exec puma'
set :rbenv_type, :system
set :rbenv_path, '/usr/local/src/rbenv'
set :rbenv_ruby, '3.0.0'
set :linked_dirs,
    fetch(:linked_dirs, []).push(
      'log',
      'tmp/pids',
      'tmp/cache',
      'tmp/sockets',
      'vendor/bundle',
      'public/system',
      'public/uploads',
    )
set :linked_files,
    fetch(:linked_files, []).push(
      'config/database.yml',
      'config/secrets.yml',
      'config/puma.rb',
      '.env',
    )

namespace :puma do
  Rake::Task[:restart].clear_actions

  desc 'Overwritten puma:restart task'
  task :restart do
    puts 'Overwriting puma:restart to ensure that puma is running. Effectively, we are just starting Puma.'
    puts 'A solution to this should be found.'
    invoke 'puma:stop'
    invoke 'puma:start'
  end

  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end
  before :start, :make_dirs
end

namespace :deploy do
  desc 'Make sure local git is in sync with remote.'
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts 'WARNING: HEAD is not the same as origin/master'
        puts 'Run `git push` to sync changes.'
        exit
      end
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting, :check_revision
  after :finishing, :compile_assets
  after :finishing, :cleanup
end

after 'deploy', 'sitemap:refresh'

Capfile

require 'capistrano/setup'

require 'capistrano/deploy'

require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
require 'capistrano/rails'
require 'capistrano/rbenv'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/bundler'
require 'capistrano/puma'
require 'capistrano/sitemap_generator'
require 'whenever/capistrano'

require 'dotenv'
Dotenv.load

install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Systemd
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source