02 March 2009

Deploying per-server crontabs with Capistrano

There's been a couple cool writeups/solutions to deploying your crontab files when you deploy with Capistrano, which I think is great. I can't find the first one I saw (mention in comments and I'll update), but on GitHub, javan has the whenever gem that is really more about allowing you to define crontabs with Ruby/Rails' time methods so you don't have to remember the crontab file syntax which none of us ever seems to be able to remember. You can of course integrate this with Capistrano (and that's covered in his Readme). The point of all this: no more having to remember to go put in or uncomment a crontab entry once you deploy a certain build, and keeping your crontabs under version control. However, for us, none of the solutions out there worked quite right, and I just use what I find to be a simpler setup.

First, we have multiple servers with different crontabs per server. Also, we have some environment variables that get defined within the crontab so that they work properly on our Engine Yard slices. I just found that, while yes, I sometimes don't remember all the crontab syntax perfectly, I also don't do this often enough for that to be an issue, and would rather just have the real deal right there, so I knew exactly what I was going to install on my server. Lastly, I didn't really want to have yet another gem dependency for something pretty straight forward like this (IMHO).

So, get on with it you say, what's the solution? Two pieces. First, I create a crontab directory within my Rails app's config directory. In that I store crontab files named by the hostname of the server - the same thing you'd get by doing a hostname on the server. You could add an extension or whatever you want, but the hostname is what makes this work easily, so you want that somewhere in your file naming convention. We only have a couple servers and I know them well, so I just went with pure hostname for now. The contents of each file are exactly what you'd see in the crontab file on the server, for the user you set it up under.

Second, a simple Capistrano task to affect the given crontab file on the server, with an after hook to run it:

task :write_crontab, :roles => :app do
puts "Installing server-specific crontab."
run("cd #{deploy_to}/current/config/crontab; crontab `hostname`")
after "deploy:restart", "write_crontab"

That's it. You can obviously tweek this for your own setup, for example, maybe you need to run it on all roles, or different roles, or what not. Your run command might need to be more robust (or run a shell script or rake task) for example if not all servers have crontabs or you have something more dynamic. But, as you can tell, setting this kind of thing up is pretty straight forward, and it's great to keep your crontab setups in version control. Thanks to you guys that stimulated the idea in the first place.


Darragh Curran said...

Hey Chris,
I ended up reaching the
same conclusion.

No need for a library, make it a 1 liner!

The trick with hostname is handy, I'll remember that.

Ben said...

Hey, neat approach, thanks. One nit-pick: I think you meant "effect" rather than "affect" in "a simple Capistrano task to affect the given crontab file on the server".

Chris said...

Ben, I meant affect, since affect, means "to have an effect on". Probably should have just said something like to load the contents of the file into the crontab :)

wikiplugs said...

Dear Blogger,

I posted a comment at your blog regarding addition of your rich content at my site wikiplugs.com and in return of providing you a free widget capable of displaying your post at random in your pages.I think you would be interested in Something which crawls more posts/stories than normal RSS limit ? (Unlike other widgets)

check some examples at,

website: www.13above.com (See the Vertical Banner Here on the right panel…)
and blog: emailbookmarking.blogspot.com (... and also Horizontal Banner Here on the top)

Just have a look and then visit http://www.wikiplugs.com/labs/index.html to grab your free copy today. (Limited Offer)

Your posts and our commitment can do wonders!

So what are you waiting for?Do join and enrich us with your great thoughts.