Skip to content

Instantly share code, notes, and snippets.

@tanitanin
Last active December 24, 2015 23:39
Show Gist options
  • Save tanitanin/6882469 to your computer and use it in GitHub Desktop.
Save tanitanin/6882469 to your computer and use it in GitHub Desktop.
Daemonsでデーモン化したスクリプトをCapistranoから起動できるようにする
##### hello.rb
#!/usr/bin/env ruby
require 'daemons'
require 'logger'
# Initialize
logpath = File.expand_path("app.log")
log = Logger.new(logpath)
log.info("Completely initialized")
# Daemonize
Daemons.run_proc("app") do
log = Logger.new(logpath)
loop do
log.info "Hello daemons world!"
sleep(5)
end
end
##### Capfile
load 'config/deploy'
##### config/deploy.rb
require "capistrano/ext/multistage"
require 'bundler/capistrano'
require "railsless-deploy"
# Application settings
set :application, "capistrano-daemons-deploy"
set :stages, %w{development}
set :default_stage, "development"
# Git repository
set :repository, "git@git-repos:#{application}.git"
set :scm, :git
set :deploy_via, :copy
# RVM settings
require 'rvm/capistrano'
set :rvm_ruby_string , "ruby-2.0.0@#{application}"
before 'deploy:setup', 'rvm:install_rvm'
before 'deploy:setup', 'rvm:install_ruby'
# Hello script daemons
namespace :hello do
desc "Start daemons script"
task :start , :roles=>:app do
run "cd #{current_path} && bundle exec ruby hello.rb start"
end
desc "Stop daemons script"
task :stop, :roles=>:app do
run "cd #{current_path} && bundle exec ruby hello.rb stop"
end
desc "Restart daemons script"
task :restart, :roles=>:app do
run "cd #{current_path} && bundle exec ruby hello.rb restart"
end
desc "Execute tail log file"
task :log, :roles=>:app do
run "cd #{current_path} && tail app.log"
end
end
##### config/deploy/development.rb
# Application server
role :app, "app_server.net"
set :user, "app_user"
set :use_sudo, false
set :deploy_to, "/home/#{user}/#{application}"
ssh_options[:port] = "22"
default_run_options[:pty] = true
##### Gemfile
# A sample Gemfile
source "https://rubygems.org"
ruby "2.0.0"
# Deploy tools
group :deploy do
gem "capistrano"
gem "capistrano-ext"
gem "capistrano_colors"
gem "railsless-deploy"
gem "rvm-capistrano"
end
# Daemons
gem "daemons"
# Logging
gem "logger"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment