Setting up Cron jobs that call Ruby (RVM) or Shell Scripts via Bash

If you are on a unix system and want to set up a crontab (cron job) and you are using RVM, you must make sure to make bash act as if it had been invoked as a login shell by using the bash -l option.

So if you were going to have a script run at midnight every 24 hours, you put something like this in your crontab. I assume the your_script.sh calls your .rb code from within it.

0 0 * * * bash -l /home/username/your_script.sh

If you wanted to call your ruby code directly from the crontab, then you would do something like this:

0 0 * * * bash -l -c /home/username/your_ruby_code.rb

Be sure your .rb file is executable and has something like the following as its first line:

#!/usr/bin/env ruby

VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)
Setting up Cron jobs that call Ruby (RVM) or Shell Scripts via Bash, 5.0 out of 5 based on 1 rating
Facebook Twitter Email

Leave a Reply