Deploying Crontab With Your Rails App

This is a short one. If cron is an old friend, don’t futz around with weird Ruby. You know the pitfalls of cron (environment variables, long jobs without lock files). So write your crontab and check it into config/crontab.

` 0 0 * * * /usr/bin/some_important_command `{lang=”sh”}

Add this to your config/deploy.rb:

` namespace :deploy do desc “Install crontab” task :crontab, :roles => :app do run “crontab #{current_path}/config/crontab” end after “deploy:symlink”, “deploy:crontab” end `{lang=”ruby”}

Check in your code. Now every time you cap deploy, your crontab will be updated on your app servers. So simple it’s not even a gem on github.

Note that if you have multiple app servers and a job that should only run once (as opposed to once per app server), you’ll need to create a role and alter the task accordingly. I guess if someone were so inclined they could make a fancy gem (and host it on github, OF COURSE) that supports multiple crontabs named after roles, but it’s late and I’m not quite so inclined.