# executing a method from command line \[cron\]

**URL:** <https://discuss.rubyonrails.org/t/executing-a-method-from-command-line-cron/5891>\
**Category:** rubyonrails-talk\
**Created:** [December 15, 2006, 11:01am UTC](https://discuss.rubyonrails.org/t/executing-a-method-from-command-line-cron/5891 "2006-12-15T11:01:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://discuss.rubyonrails.org/u/Rajkumar_S)\
**Post date:** [December 15, 2006, 11:01am UTC](https://discuss.rubyonrails.org/t/executing-a-method-from-command-line-cron/5891/1 "2006-12-15T11:01:22Z")

</div>

Hi,

I need to do some database housekeeping periodically. Right now I have a method in my model, which I am calling from ./script/console like x = Model.update\_all and it works fine.

I need to put this in cron. I know that I should be using ./script/runner, I have looked a lot for an example script encapsulating my method to do this. But could not find any. It would be great if some one can post a simple script that I can call from cron.

I am also not sure if ./script/runner calls methods in my controller or model.

thanks and regards,

raj

---

<div class="post-metadata">

**Author:** ![Jason\_Norris](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@Jason\_Norris](https://discuss.rubyonrails.org/u/Jason_Norris)\
**Post date:** [December 15, 2006, 2:25pm UTC](https://discuss.rubyonrails.org/t/executing-a-method-from-command-line-cron/5891/2 "2006-12-15T14:25:48Z")

</div>

script/runner will run model methods like this script/runner -e production 'Emailer.send\_daily\_announcements'

Be aware that script/runner loads the Rails framework every time it runs, so it starts slow. I.E. don't use it for jobs that you run all the time. For a database monitoring daemon that runs a simple query every 5 seconds, I use a daemon that starts with './script/runner .... &' and runs constantly - the method that I call has an infinite loop. This way the environment only loads once.

Jason

Rajkumar S wrote:

---

<div class="post-metadata">

**Author:** ![Justin\_Forder1](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Justin\_Forder1](https://discuss.rubyonrails.org/u/Justin_Forder1)\
**Post date:** [December 15, 2006, 9:45pm UTC](https://discuss.rubyonrails.org/t/executing-a-method-from-command-line-cron/5891/3 "2006-12-15T21:45:54Z")

</div>

Peter wrote:

> If you are on a Mac the following:
> 
> couldn't get it to work either.
> 
> so
> 
> check this guys post because I owe him a Heineken
> 
> [http://uberhamster.com/2006/9/12/invoking-ruby-on-rails-commands-via-cron-and-script-runner](http://uberhamster.com/2006/9/12/invoking-ruby-on-rails-commands-via-cron-and-script-runner)
> 
> Now if someone has the answer why its got to be a full path let us know

For DreamHost, and I expect for you too, the answer is that cron jobs are run with no environment (so no PATH). See here:

&nbsp;&nbsp;&nbsp;[http://wiki.dreamhost.com/index.php/Crontab](http://wiki.dreamhost.com/index.php/Crontab)

regards

&nbsp;&nbsp;&nbsp;Justin Forder

---

<div class="post-metadata">

**Author:** ![Rajkumar\_S](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@Rajkumar\_S](https://discuss.rubyonrails.org/u/Rajkumar_S)\
**Post date:** [December 16, 2006, 6:07am UTC](https://discuss.rubyonrails.org/t/executing-a-method-from-command-line-cron/5891/4 "2006-12-16T06:07:18Z")

</div>

Thanks every one for the helpful mails! I am now able to run the Model method from command line.

raj
