CAP CRONTAB INSTALLER

Capistrano recipe for installing crontabs from the config/crontabs directory in your application. Extremely simple, but flexible. Crontab files are evaluated as ERB templates and have access to all capistrano variables.

INSTALLATION

# in Gemfile:
gem 'cap_crontab'

$ bundle install

# in your config/deploy.rb:
require 'cap_crontab'

Create a directory in config/ named crontabs/ and create crontab files for each environment where you want to install a crontab:

config/
  crontabs/
    production
    staging

Example crontab file:

# Do some stuff once an hour
0 * * * * RAILS_ENV=<%= rails_env %> ruby <%= current_path %>/script/cron/do_stuff.rb
# Do some other stuff every twenty minutes
*/20 * * * * RAILS_ENV=<%= rails_env %> ruby <%= current_path %>/script/cron/do_other_stuff.rb

This file is evaluated as an ERB template and all of the existing capistrano variables are available for use.

In your deploy file add the :crontab => true flag on the server(s) where you’d like the crontab installed:

task :staging do
  set :env, "staging"
  role :web,                  'example.com'
  role :app,                  'example.com', :crontab => true
  role :db,                   'example.com', :primary => true
end

Add an after hook to install the crontab on the server on deploy:

after "deploy:symlink", "crontab:install"

USAGE

If you use the ‘after’ hook the crontab will be installed on each deploy.

Otherwise to install it separately:

cap crontab:install

To show the crontab after installing it:

cap crontab:show

Copyright © 2010 Conor Hunt <conor.hunt AT gmail> Released under the MIT license