Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
34
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Rakefileにunicorn起動・停止のコマンドを追加する

いい加減$ bundle exec unicorn_rails...をやるのが面倒なので、Rakefileにunicorn起動・停止のタスクを追加した。

Rakefile
require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

namespace :unicorn do

  ##
  # Tasks
  ## 
  desc "Start unicorn"
  task(:start) {
    config = rails_root + "config/unicorn.rb"
    sh "bundle exec unicorn_rails -c #{config} -E development -D"
  }

  desc "Stop unicorn"
  task(:stop) { unicorn_signal :QUIT }

  desc "Restart unicorn with USR2"
  task(:restart) { unicorn_signal :USR2 }

  desc "Increment number of worker processes"
  task(:increment) { unicorn_signal :TTIN }

  desc "Decrement number of worker processes"
  task(:decrement) { unicorn_signal :TTOU }

  desc "Unicorn pstree (depends on pstree command)"
  task(:pstree) do
    sh "pstree '#{unicorn_pid}'"
  end

  ##
  # Helpers
  ##
  def unicorn_signal signal
    Process.kill signal, unicorn_pid
  end

  def unicorn_pid
    begin
      File.read("/tmp/unicorn.pid").to_i
    rescue Errno::ENOENT
      raise "Unicorn doesn't seem to be running"
    end
  end

  def rails_root
    require "pathname"
    Pathname.new(__FILE__) + "../"
  end 
end

これで、$ rake unicorn:startで、unicornの起動。
$ rake unicorn:stopでunicornの停止が出来る。

unicorn.pidをrails/tmp/pidsにおいている人は、/tmp/unicorn.pidのpathを変更したらいけるはず。

参考

Rake tasks for Unicorn: start stop restart increment decrement pstree

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
34
Help us understand the problem. What are the problem?