RVM, Bundler and Cron in Production: Round 2

Back in 2010, we suggested using /bin/bash -l -c to run scout via Cron when using RVM. However, this was a brute approach: /bin/bash -l -c tells bash to behave as a login, interactive process. However, as Daniel Szmulewicz elequently stated in the comments for the original blog post, "Cron jobs are by nature non-login, non-interactive processes".

Fast-forward to today: RVM usage is continuing in production, and to make things more complicated, Cron jobs often need to account for both RVM and Bundler. So, what's our preferred approach when running Ruby executables via Cron in an RVM, RVM+Bundler, or Bundler environment? A shell script.

Cron Shell Script: RVM + Bundler

Lets say we want to run a Ruby executable (scout [KEY]) via Cron with (1) Ruby 1.9.2 and (2) my Rails App's Gem bundle:

Make the shell script executable: chmod +x FILE.sh.

Add the Cron job:

* * * * * shell_script.sh

But that's a lot of typing...

It's tempting to use /bin/bash -l -c when you are busy/lazy. To get around this, the scout install [KEY] command will detect if you are using (1) RVM and/or (2) Bundler. If so, we generate the shell script for you and make it executable.

scout install BNrIneEBMwE8h6VlhO4Bw4WmOVSLmnygSFZEPCfi
=== Scout Installation Wizard ===

It looks like you've installed Scout under RVM and/or Bundler. 
We've generated a shell script for you.

Run `crontab -e`, pasting the line below into your Crontab file:

* * * * * /Users/dlite/.scout/scout_cron.sh

How do we detect RVM and Bundler? We've encapsulated it into an Environment class: