# ruby script - how to write one that deals with different environment?

**URL:** <https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686>\
**Category:** rubyonrails-talk\
**Created:** [April 6, 2010, 7:32am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686 "2010-04-06T07:32:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordon\_Yeong](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Gordon\_Yeong](https://discuss.rubyonrails.org/u/Gordon_Yeong)\
**Post date:** [April 6, 2010, 7:32am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/1 "2010-04-06T07:32:02Z")

</div>

hi guys,

I would like to write a script that is runned by cron. It will update multiple entries.

Any guides for this? I can't figure out how do we choose which ruby environment (ie. development/test/production) in the script and also, how do I access models in the script (which is also used by my rails application)?

thanks

---

<div class="post-metadata">

**Author:** ![Andy\_Jeffries1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Andy\_Jeffries1](https://discuss.rubyonrails.org/u/Andy_Jeffries1)\
**Post date:** [April 6, 2010, 9:21am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/2 "2010-04-06T09:21:08Z")

</div>

The easiest way may be to have a class with a class method, e.g.

class FooRunner

def self.run

…

end

end

And then call:

/path/to/my/app/script/runner -e production FooRunner.run

from Cron.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman1](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@Peter\_Hickman1](https://discuss.rubyonrails.org/u/Peter_Hickman1)\
**Post date:** [April 6, 2010, 9:37am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/3 "2010-04-06T09:37:33Z")

</div>

All you need is access to the RAILS\_ENV constant. For example this script

– x.rb–

puts RAILS\_ENV

---

<div class="post-metadata">

**Author:** ![Gordon\_Yeong](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Gordon\_Yeong](https://discuss.rubyonrails.org/u/Gordon_Yeong)\
**Post date:** [April 6, 2010, 9:40am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/4 "2010-04-06T09:40:49Z")

</div>

Cool but how abt accessing the models? Andy gave a solution but i want to see what others think

---

<div class="post-metadata">

**Author:** ![Andy\_Jeffries1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Andy\_Jeffries1](https://discuss.rubyonrails.org/u/Andy_Jeffries1)\
**Post date:** [April 6, 2010, 9:53am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/5 "2010-04-06T09:53:48Z")

</div>

You should just be able to access the models normally (i.e. just use the class, without needing to require anything) if you’re using script/runner.

If you want to do it with a raw Ruby script (rather than using script/runner), you’ll have to include RubyGems, ActiveRecord and point ActiveRecord at the database.yml section, load all the models found below the specific part.

To be honest, it’s much easier just to use script/runner.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman1](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@Peter\_Hickman1](https://discuss.rubyonrails.org/u/Peter_Hickman1)\
**Post date:** [April 6, 2010, 9:55am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/6 "2010-04-06T09:55:02Z")

</div>

If you use script/runner then just use them as you normal would.

Scripts run from script/runner run as normal rails application so all the same setup that the web application has will be provided for your script.

---

<div class="post-metadata">

**Author:** ![Gordon\_Yeong](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Gordon\_Yeong](https://discuss.rubyonrails.org/u/Gordon_Yeong)\
**Post date:** [April 6, 2010, 10:55am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/7 "2010-04-06T10:55:14Z")

</div>

hi, Andy and Peter, Thanks for the feedback 🙂i will try it out 🙂

Regards, Gordon Yeong

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 6, 2010, 11:44am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/8 "2010-04-06T11:44:47Z")

</div>

> If you use script/runner then just use them as you normal would.

Just out of curiosity couldn't you just to a rake task instead and call that from cron?

---

<div class="post-metadata">

**Author:** ![Gordon\_Yeong](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Gordon\_Yeong](https://discuss.rubyonrails.org/u/Gordon_Yeong)\
**Post date:** [April 6, 2010, 11:49am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/9 "2010-04-06T11:49:15Z")

</div>

guys, in addition to Peter’s reply, here’s a good reference for script/runner.

---

<div class="post-metadata">

**Author:** ![bseanvt](https://avatars.discourse-cdn.com/v4/letter/b/7ea924/32.png) [@bseanvt](https://discuss.rubyonrails.org/u/bseanvt)\
**Post date:** [April 6, 2010, 1:50pm UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/10 "2010-04-06T13:50:45Z")

</div>

If you set up a rake task in lib/tasks/your\_cron\_job.rake with something like this

namespace :your\_cron\_job do &nbsp;&nbsp;&nbsp;desc "describe the task" &nbsp;&nbsp;&nbsp;task :every\_five\_minutes =\> :environment do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@post = Post.new &nbsp;&nbsp;&nbsp;end end

you can add it manually to the cron by typing 'crontab -e' which opens the file for editing (\*nix only) crontab -e

first specify how often you want the job to run... in this case every 5 minutes "\*/5 \* \* \* \*" along with correct paths to app and rake, set the RAILS\_ENV and task name to execute. Cron is just a file so you'll enter all the info for the job on one line like so... (if you don't have a default editor set up already it will prompt you to do so before entering in this command).

\*/5 \* \* \* \* cd /var/www/my\_ror\_application && /usr/bin/rake RAILS\_ENV=production your\_cron\_job:every\_five\_minutes

More info here [Seanbehan.com](http://seanbehan.com/programming/set-cron-job-to-run-every-five-minutes-for-a-ruby-on-rails-rake-task/) There are more elegant approaches to this problem. This is just the quick and dirty.

---

<div class="post-metadata">

**Author:** ![Gordon\_Yeong](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Gordon\_Yeong](https://discuss.rubyonrails.org/u/Gordon_Yeong)\
**Post date:** [April 8, 2010, 5:49am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/11 "2010-04-08T05:49:51Z")

</div>

hi there, bseanvt,

&nbsp;&nbsp;&nbsp;The thought of using the rake task is good but it's not applicable to my situation whereby I am just running this script after each run of thinking\_sphinx:index which takes place every 20 mins.

---

<div class="post-metadata">

**Author:** ![Gordon\_Yeong](https://avatars.discourse-cdn.com/v4/letter/g/e47c2d/32.png) [@Gordon\_Yeong](https://discuss.rubyonrails.org/u/Gordon_Yeong)\
**Post date:** [April 8, 2010, 5:50am UTC](https://discuss.rubyonrails.org/t/ruby-script-how-to-write-one-that-deals-with-different-environment/50686/12 "2010-04-08T05:50:52Z")

</div>

[http://www.ameravant.com/posts/recurring-tasks-in-ruby-on-rails-using-runner-and-cron-jobs](http://www.ameravant.com/posts/recurring-tasks-in-ruby-on-rails-using-runner-and-cron-jobs)
