Most of us use Resque and the Resque or Rufus schedulers to schedule tasks in an effective, controlled manner. However, we sometimes need to schedule jobs on machines that don't have a queue installed. We then have to fall back on good ol' raw Cron scheduling. The problem here is that Cron jobs usually run under the root user's environment, which means that the RVM environment variables necessary for running our Ruby script aren't being loaded, so the Cron job will fail.
The solution is to explicitly load the RVM environment in the cron user's context, before we can invoke our Ruby code. Luckily, RVM provides environment files for each configuration, that we can use to load the right context. These files are essentially a list of exported env variables that point to the right locations for the selected Ruby and gemsets.
1. Find the RVM environment file for your ruby version and gemset. You can do so by running:
Which will return something like
2. Create a shell script (let's call it loader.sh) that does the following:
- sources the RVM environment file
- runs "bundle install"
- calls your Ruby script.
3. Run "$ crontab -e" and add a line to invoke your shell script, e.g.
Your Ruby script should now run at 1 minute past midnight every Sunday.