2

I'm having a rough time executing script/runner with a cron and RVM. I believe the issues lie with the rvm environment not being set before the runner is executed.

currently im throwing the error /bin/sh: 1.sql: command not found which is more than i've gotten earlier, so i guess that's good.

I've read this thread Need to set up rvm environment prior to every cron job but im still not really getting it. Part of the problem i think is the error reporting.

this is my runner thus far.

 */1 * * * * * /bin/bash -l -c 'rvm use 1.8.7-p352@2310; cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"'

as per the above link, i tried making a rvm_cron_runner.

i created a file and placed this in it:

#!/bin/sh
source "/Users/dude/.rvm/scripts/rvm"
exec $1

then i updated my crontab to this.

*/1 * * * * * /bin/bash -l -c '/Users/dude/development/app/my_app2310/rvm_cron_runner; rvm use 1.8.7-p352@2310; cd development/app/my_app2310 && script/runner -e development "Mailer.find_customer"'

This also has made no difference. i get no error. nothing.

Can anyone see what i'm doing incorrectly?

P.S i hope my code formatting worked.

3 Answers 3

5

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.

2
  • hmm. unfortunately it didn't appear to make a difference. I've got three files now. cron_job, rvm_cron_runner and cron_test.txt. cron_test.txt is executed via crontab crontest.txt. there also appears to be no error to report back.
    – that_guy
    Aug 28, 2011 at 8:07
  • i got this working. it had to do with how i was executing crontab. thank you.
    – that_guy
    Aug 29, 2011 at 4:40
4

You don't need to write a second cron runner (following that logic, you might as well write a third cron runner runner). 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

0

You can use rvm wrappers:

/home/deploy/.rvm/wrappers/ruby-2.2.4/ruby

Source: https://rvm.io/deployment/cron#direct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.