How to Write Capistrano Task for Non Rails

Because capistrano is so powerful, sometimes, we want to use it with other things other than Rails. Here is a quick example.

I want to get a single command line output on my remote host e.g. free -m

  • Install capistrano

      $ vi Gemfile
    
          source 'https://rubygems.org'
          gem 'capistrano', '~> 2.15'
    
      $ bundle install
    
  • Generate capistrano

      $ capify .
    
  • Create a task

      $ vi config/deploy.rb
    
          set :application, "my-app-ios"
    
          set :user, "chawarong.s"
          set :host, "my.example.com"
    
          role :web, "#{host}"                          # Your HTTP server, Apache/etc
          role :app, "#{host}"
    
          namespace :monitor do
            task :free do
              output = capture "free -m"
              puts output
    
            end
          end
    
  • Run it

      $ cap monitor:free
        * 2014-01-09 15:36:47 executing `monitor:free'
        * executing "free -m"
          servers: ["my.example.com"]
          [my.example.com] executing command
          command finished in 376ms
                   total       used       free     shared    buffers     cached
      Mem:          2003       1845        158          0        183        984
      -/+ buffers/cache:        677       1326
      Swap:          507          0        507
    
    
    
      ~