# Cron w/ Rails

**URL:** <https://www.ruby-forum.com/t/cron-w-rails/66387>\
**Category:** Rails\
**Created:** [August 25, 2006, 3:21am UTC](https://www.ruby-forum.com/t/cron-w-rails/66387 "2006-08-25T03:21:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 25, 2006, 3:21am UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/1 "2006-08-25T03:21:39Z")

</div>

I’m trying to setup a number of recurring processes and based on the  
input of others have found my way to cron. I’ve made a number of  
attempts to setup jobs to perform any number of task, always receiving  
error messages. I’ve come across requests by others with the identical  
problem, but I’ve yet to find one that has been responded to.

I’m running OS X 10.4 with all the latest Ruby/Rails installs.

If I execute the following line in a Terminal window I get the expected  
result “Testing”  
ruby /Users/stocad/rrprojects/testproject/script/runner -e development  
“puts ‘Testing’”

I’ve put the following line into my crontab:  
30 \* \* \* \* ruby /Users/stocad/rrprojects/testproject/script/runner -e  
development “puts ‘Testing’”

After the perscribed time has arrived (30 after any hour) I check my  
email (I put a .forward file in my home directory as one of the  
tutorials I was following suggested). My email contains the following  
message:

/Users/stocad/rrprojects/mycms/script/…/config/boot.rb:18:in  
`require': No such file to load -- rubygems (LoadError) from /Users/stocad/rrprojects/testproject/script/../config/boot.rb:18 from /Users/stocad/rrprojects/testproject/script/runner:2:in`require’  
from /Users/stocad/rrprojects/testproject/script/runner:2

There are a good number of resources out there telling me what to do if  
I’m missing rubygems altogether, but rubygems seems to be working for  
me in all cases except when I attempt to run a ruby script through  
cron.

Any assistance would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 25, 2006, 4:11am UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/2 "2006-08-25T04:11:43Z")

</div>

I’ve never used script/runner in a cron job, but I have used cron  
with Rails.

I put bitsweat\_wears\_sneakers.rb in lib (or wherever). After the  
shebang, I plop in

require ‘/full/path/to/my/awesome/app/config/environment.rb’

The invocation would be something like

30 \* \* \* \* ruby /Users/stocad/rrprojects/testproject/lib/  
bitsweat\_wears\_sneakers .rb

The full path thingee isn’t too portable if your dev and production  
setups are different. You could do some checking to see whether …/  
config/environment.rb (or some other environment.rb) exists and  
require it if it does.

Is there an easier way, list?

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 25, 2006, 8:48am UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/3 "2006-08-25T08:48:11Z")

</div>

you could also use BackgrounDRb for that purpose, since it kind-of  
emulates  
cron.  
altough it can do many many other things

2006/8/25, Aaron S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If I execute the following line in a Terminal window I get the expected  
> After the perscribed time has arrived (30 after any hour) I check my  
> from /Users/stocad/rrprojects/testproject/script/runner:2:in
> 
> >

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.siebert-wd.de](http://www.siebert-wd.de) - Gedanken lesen  
[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 25, 2006, 9:20am UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/4 "2006-08-25T09:20:13Z")

</div>

cron runs in a very limited environment, without all the environment  
variables set like you have in a login session. You probably need to  
add something to the load path, although I haven’t used rails via cron  
so I can’t tell you specifically what to do. That’s where to look  
though.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 25, 2006, 9:00am UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/5 "2006-08-25T09:00:11Z")

</div>

Zach, I tried running with an rb file first, and created an  
arrangement much as you have described. This was where I initially  
came across the error message. After poking around I came to the  
conclusion that the runner script was the environment setup for quickly  
issuing single commands, which is really what I want to do anyway.  
Either way the error message I posted occurs in poth situations.

Michael, BackgrounDRb to my understanding was created to issue long  
running processes without delaying a pages load. I guess I could adapt  
that to work, but if I understand correctly the best tool for issuing  
batch style processing is cron.

I don’t mean to sound ungrateful, but if possible I’d prefer to figure  
out why it isn’t working rather than installing a work around.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 26, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/6 "2006-08-26T13:04:21Z")

</div>

Aaron,

I just tried the script/runner approach. It worked, the differences  
being the path and the frequency.

Can you tell cron to send you the location of the ruby it uses? Like  
by running

which ruby

Then make sure it’s the ruby you think it is?

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 25, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/7 "2006-08-25T12:38:35Z")

</div>

2006/8/25, Aaron [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Michael, BackgrounDRb to my understanding was created to issue long  
> running processes without delaying a pages load. I guess I could adapt  
> that to work, but if I understand correctly the best tool for issuing  
> batch style processing is cron.

With the new autostart feature, a worker can be started when  
backgroundrb is  
started and with repeat\_every class method you can control how often the  
job  
is executed. this works great for me and i think its good not having to  
administer additional software.

I don’t mean to sound ungrateful, but if possible I’d prefer to figure

> out why it isn’t working rather than installing a work around.

you could also possibly try to use a rake task like this:

task :name =\> :environment do  
blah  
end

this way you loaded your environment (determined by ENV[‘RAILS\_ENV’])  
and  
have access to all other rails-specific stuff. I’d prefer that since i  
think  
it can be tested better (and i never used runner)

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.siebert-wd.de](http://www.siebert-wd.de) - Gedanken lesen  
[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/8 "2006-09-04T14:46:19Z")

</div>

Zach, It worked. I used “which ruby” to compare the environments  
inside and outside of cron (they where different). I just had to set a  
different PATH within my crontab to get it to use the correct install  
of ruby. Once I added that line it works like a charm. Thanks  
everyone for all your assistance.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/cron-w-rails/66387/9 "2006-09-04T14:46:27Z")

</div>

Mazel tov.
