Cron w/ Rails

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.

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?

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

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.

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.

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)

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?

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.

Mazel tov.