While nothing is wrong with Unix cron, it is not the best choice for regular jobs that require to be run in the context of your Rails application. Spinning up an instance of the application every 10 minutes for a job that needs to run this frequently is just a waste of CPU cycles, let alone the increased memory usage in case different jobs are running in parallel.

Building blocks

Cronic itself is only a tiny bit of glue code and a Rails generator.

The scheduling itself is done by Rufus Scheduler . I like this particular library because it allows you fine grained control over which jobs may be run in parallel and which have to be run in sequence via mutexes. It also can avoid overlapping with long running jobs. These are things that you have to take care of yourself when using Unix cron. The daemonizing part is provided by Dante, which is amazingly easy to use and 'just works'. Optional Airbrake integration is also included.


Three easy steps:

Add Cronic to your Gemfile and run Bundler

echo "gem 'cronic'" >> Gemfile
bundle install

Run the Rails generator and create job definitions

rails g cronic

This will set up script/cronic, which you will use to start / stop the daemon. It also creates the config/cronic.d directory where you will store your job definitions. Have a look at config/cronic.d/sample.rb to get an idea of how to define your jobs. For more information, be sure to visit the Rufus-Scheduler documentation. Every method that is available on a Rufus::Scheduler instance can be called in the job definition files located in config/cronic.d.

Run it

script/cronic -d -l log/cronic.log -P tmp/pids/

This will run cronic daemonized, logging to log/cronic.log, with a pid file located in tmp/pids. To run in the forground for testing purposes, just run the script without any parameters.

In order to stop the daemon, run

script/cronic -k -P tmp/pids/

Error handling

Any exception thrown during job execution will be caught and logged to STDOUT (which goes into the log file specified on the command line). If you have Airbrake set up for your application, exceptions will also be reported via Airbrake.notify.


In order to stop / start / restart Cronic automatically when deploying with capistrano, follow these steps:

Include the Cronic recipes in your Capfile or deploy.rb

require 'cronic/recipes'

Hook Cronic tasks to Capistrano's tasks

after "deploy:stop",    "cronic:stop"
after "deploy:start",   "cronic:start"
after "deploy:restart", "cronic:restart"

Optional: customize task behaviour

set :rails_env, 'production'  # this is the default

# relative to current_path, defaults to log/cronic.log
set :cronic_log, 'some/log/file'

# relative to current_path, defaults to tmp/pids/
set :cronic_pid, 'some/pid/file'

# custom role to have it run on a special server
role :cron, 'dedicated.cron.server'
set :cronic_server_role, :cron


The Dante docs have an example god script that you can use as a starting point for ensuring your cronic daemon stays up and running.


Copyright 2012 Jens Krämer, See LICENSE for details.