Cron jobs for web applications

by

I always dislike setting up cron jobs for web applications. The whole application lives in /var/www but I manually create this single line somewhere else that I have to remember to setup if I switch servers, and turn off if needed, and maintain accordingly. Well, it so happens that it can be done much better with the tools we already have: the usual cron and capistrano (or any other similar release tool).

In Restraq, a project I’m working on, I created a file named crontab in the config directory with these contents:

MAILTO=pupeno@restraq.com
*/5 * * * * www-data cd /var/www/restraq/current && RAILS_ENV=production rake mail:get

Cron can read from some predefine files but it also reads every file in /etc/cron.d/. That’s a very common Linux pattern, the files in the directory with the .d extension are read as if they were one big configuration file. Other programs do it too.

Then it’s only a matter of installing it there on deployment. At first I’ve tried with a symlink but it seems cron ignores symlinks so I decided to copy it with this line on my deploy.rb file:

run "#{try_sudo} cp #{current_path}/config/crontab /etc/cron.d/restraq"

and that’s it. No more fragmentation. Whenever, wherever I install my web app, there goes the cron jobs.


One response to “Cron jobs for web applications”

  1. masone Avatar

    I use whenever for cronjobs. I’ts pretty awesome and comes with a Capistrano task.
    http://github.com/javan/whenever

Leave a Reply

You may also like:

If you want to work with me or hire me? Contact me

You can follow me or connect with me:

Or get new content delivered directly to your inbox.

Join 5,043 other subscribers

I wrote a book:

Stack of copies of How to Hire and Manage Remote Teams

How to Hire and Manage Remote Teams, where I distill all the techniques I've been using to build and manage distributed teams for the past 10 years.

I write about:

announcement blogging book book review book reviews books building Sano Business C# Clojure ClojureScript Common Lisp database Debian Esperanto Git ham radio history idea Java Kubuntu Lisp management Non-Fiction OpenID programming Python Radio Society of Great Britain Rails rant re-frame release Ruby Ruby on Rails Sano science science fiction security self-help Star Trek technology Ubuntu web Windows WordPress

I've been writing for a while:

Mastodon

%d bloggers like this: