Rvm Isnt Setting Environment with Cron

RVM isnt setting environment with cron

Could you try to place the code you want to run in a separate script, and then use the rvm_cron_runner ?

So place your actions in a file called /path/cron_job

rvm use 1.8.7-p352@2310
cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"

and then in your crontab write

1 2 * * * /path/rvm_cron_runner /path/cron_job

The differences:

  • this does not start a separate shell
  • use the parameter of the rvm_cron_runner

If you would use an .rvmrc file, you could even drop the rvm use ... line, I think.

Need to set up rvm environment prior to every cron job

You don't need to write wrappers (following that logic, you might as well write a wrapper to the wrapper). Please keep things simple. All you need to do is configure your cron job to launch a bash shell, and make that bash shell load your environment.

The shebang line in your script should not refer directly to a ruby executable, but to rvm's ruby:

#!/usr/bin/env ruby

This instructs the script to load the environment and run ruby as we would on the command line with rvm loaded.

On many UNIX derived systems, crontabs can have a configuration section before the actual lines that define the jobs to be run. If this is the case, you would then specify:

SHELL=/path/to/bash  

This will ensure that the cron job will be spawned from bash. Still, your environment is missing, so to instruct bash to load your environment, you will want to add to the configuration section the following:

BASH_ENV=/path/to/environment (typically .bash_profile or .bashrc) 

HOME is automatically derived from the /etc/passwd line of the crontab owner, but you can override it.

HOME=/path/to/home

After this, a cron job might look like this:

15 14 1 * *     $HOME/rvm_script.rb

What if your crontab doesn't support the configuration section? Well, you will have to give all the environment directives in one line, with the job itself. For example,

15 14 1 * * export BASH_ENV=/path/to/environment && /full/path/to/bash -c '/full/path/to/rvm_script.rb'

Full blog post on the subject

Gem not found in Ruby cron job in RVM env

I configured several different operating systems to work with a couple of CRON flavors and RVM.

I first tried RVM's official solution to the problem but didn't work under FreeBSD and Gentoo. I had to manually add all relevant paths as showed bellow but first type crontab -e in order to launch the crontab editor[1]:

# atmat's crontab configuration
SHELL=/bin/bash
PATH=/home/atma/.rvm/gems/ruby-1.9.3-p0/bin:/home/atma/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/atma/.rvm/rubies/ruby-1.9.3-p0/bin:/home/atma/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i486-pc-linux-gnu/gcc-bin/4.5.3
RUBYLIB=/home/atma/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1
GEM_HOME='/home/atma/.rvm/gems/ruby-1.9.3-p0'
GEM_PATH='/home/atma/.rvm/gems/ruby-1.9.3-p0:/home/atma/.rvm/gems/ruby-1.9.3-p0@global'
RUBYOPT=rubygems

%nightly,mail(no) * 8-9 /home/atma/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /usr/local/bin/morula -s username update

The above example is working under Gentoo GNU/Linux using fcron a more flexible, beautiful and powerful solution to standard cron, but will work with any cron.

[1] This command will open crontab with your default system editor.

Can't execute Rails rake task with cron

The simplest way to use rvm with cron is to use rvm's wrappers. Your shell sets up a whole bunch of rvm-related environment when it starts, and that's missing from your cron job. The wrappers are versions of the Ruby-related commands that take care of this for you.

In this case, if you have rvm installed to /usr/local, your cron job should look something like this:

* * * * * cd /media/sf_visa-tracker/ && /usr/local/rvm/wrappers/ruby-2.1.0/bin/rake parse RAILS_ENV=production >> /var/log/visa-parse.log 2>&1

You could also bundle up your rvm setup into a shell script that loads rvm before invoking rake; there's more details of both approaches in rvm's documentation on working with cron.

why #!/usr/bin/env ruby doesn't work in crontab?

If you installed ruby via rvm, ruby probably isn't in /usr/bin. Depending on where rvm is installed:

bash -c "source /usr/local/lib/rvm" && rails runner foo.bar

You probably added a source */rvm to your bashrc that is the correct rvm loading script.



Related Topics



Leave a reply



Submit