Executing rake tasks with cron

Hi,

I have a kind of problems with executing rake tasks as cron jobs. These
problems began after upgrading to Rails 2.0.2.

So, these are the errors that I have

Cannot find gem for Rails ~>2.0.2.0:
Install the missing gem with ‘gem install -v=2.0.2 rails’, or
change environment.rb to define RAILS_GEM_VERSION with your desired
version.

I have this setup:

a bash script:

[bin]# cat promote_from_queue_rake.sh
#!/bin/bash
cd /var/apps/myapp/current
rake cron:promote_stories_from_queue RAILS_GEM_VERSION=2.0.2

This script is executed by a cron job and I get the above errors. In
fact, the code in the rake task executes, however I can not figure out
why that problems occure.

I have Rails get 2.0.2 installed and this line in my environment.rb:
RAILS_GEM_VERSION = ‘2.0.2’ unless defined? RAILS_GEM_VERSION

If someone has any ideas how to stop those errors, plese help.

10x in advance

You can start by running the rake with ‘-t’ to see where exactly it
fails and to see the backtrace.
Then I would put some “puts RAILS_GEM_VERSION” in a couple of places
(starting with before and after that line from environment.rb).
(you need to redirect the rake output to some log file to see those
prints.)

On Jan 14, 11:12 am, Stanislav B. <ruby-forum-incom…@andreas-

Well, if I run the task manually from the command line there’re no
errors. I receive that errors my mail. Only when the task has been run
by crond.

*/10 * * * * mongrel /var/apps/myapp/bin/promote_from_queue_rake.sh

Maybe the problem is somewhere in cron’s environment ?

Thank U Jason, I’ll try it.

What about the variables that are set in environment.rb?
I thought that “RAILS_GEM_VERSION = ‘2.0.2’ unless defined?
RAILS_GEM_VERSION” (in env.rb) is enough.

I have a full path to the bash script in my crontab.
*/10 * * * * mongrel /var/apps/myapp/bin/promote_from_queue_rake.sh

Cron by default has a very minimalist environment. If you need something
like RUBY_INCLUDE_PATH, you’ll need to add it to your crontab. And it is
good practice to either specify the PATH you need, or to type in the
full
path of the executable you are trying to run.

Jason

On Jan 14, 2008 8:32 AM, Stanislav B. <

If it works ok when you run directly, but not from cron, then for
sure, you need to spell out the full paths of all commands: both the
command run from crontab, and the commands inside your script. So -
if you run rake inside your script, then it will need
/full/path/to/rake…

Tony

On 14/01/2008, Stanislav B. [email protected]
wrote:

Posted via http://www.ruby-forum.com/.


Tony White

Hi again,

My crontab looks like this, now:

RAILS_GEM_VERSION=2.0.2
0 * * * * mongrel /var/apps/svejo.net/bin/promote_stories_rake.sh

The rake task executes successfully, however I continue to receive
errors on my mail. Like those:

Cannot find gem for Rails ~>2.0.2.0:
Install the missing gem with ‘gem install -v=2.0.2 rails’, or
change environment.rb to define RAILS_GEM_VERSION with your desired
version.

I’ve defined RAILS_GEM_VERSION in the environment.rb configuration, so I
just don’t have any ideas how too fix this.

Any ideas and help are appreciated.

Well, as a normal user, I don’t have RAILS_GEM_VERSION exported and
don’t get those errors.

In fact I’m passing that var as an input for the rake task:

#!/bin/bash
cd /var/apps/myapp/current
rake cron:promote_stories_from_queue RAILS_GEM_VERSION=2.0.2

I’ll continue to experiment. However I just don’t have any ideas what to
test…

oh - and you can’t put stuff in crontab like RAILS_GEM_VERSION=2.0.2 -
that must be in your script, and exported

T

On 14/01/2008, Stanislav B. [email protected]
wrote:

Posted via http://www.ruby-forum.com/.


Tony White
SpeechNet Technologies Ltd

Tony, thank you for your answer.

In fact I stopped receiving those error messages. However, I can not
give a clear explanation to myself about this problem. Why it did
occurred…

If I understand something more, I’ll write here for the help of someone
else.

on my system:
tony@gutsy:~$ whereis rake
rake: /usr/bin/rake

so you need:
#!/bin/bash
cd /var/apps/myapp/current
/usr/bin/rake cron:promote_stories_from_queue RAILS_GEM_VERSION=2.0.2

if your rake is in the same place as mine.

Best of luck,
Tony

On 16/01/2008, Stanislav B. [email protected]
wrote:

I’ll continue to experiment. However I just don’t have any ideas what to
test…

Posted via http://www.ruby-forum.com/.


Tony White

Stanislav, were you able to figure out why it was occuring in the first
place?