# Executing rake tasks with cron

**URL:** <https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969>\
**Category:** Deployment\
**Created:** [January 14, 2008, 10:12am UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969 "2008-01-14T10:12:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 10:12am UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/1 "2008-01-14T10:12:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/2 "2008-01-14T12:02:43Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/3 "2008-01-14T14:32:06Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/4 "2008-01-14T16:37:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 3:09pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/5 "2008-01-14T15:09:41Z")

</div>

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. \<

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 11:28pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/6 "2008-01-14T23:28:39Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Tony White

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/7 "2008-01-14T17:36:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 15, 2008, 10:06pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/8 "2008-01-15T22:06:52Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 14, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/9 "2008-01-14T23:31:35Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Tony White  
SpeechNet Technologies Ltd

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 21, 2008, 9:46am UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/10 "2008-01-21T09:46:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [January 15, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/11 "2008-01-15T23:11:03Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## I’ll continue to experiment. However I just don’t have any ideas what to test…
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Tony White

---

<div class="post-metadata">

**Author:** ![stanislavb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@stanislavb](https://www.ruby-forum.com/u/stanislavb)\
**Post date:** [June 8, 2008, 4:57am UTC](https://www.ruby-forum.com/t/executing-rake-tasks-with-cron/125969/12 "2008-06-08T04:57:59Z")

</div>

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