# Cron and rails

**URL:** <https://www.ruby-forum.com/t/cron-and-rails/106384>\
**Category:** Deployment\
**Created:** [June 9, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384 "2007-06-09T14:13:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/1 "2007-06-09T14:13:11Z")

</div>

Hi, i’ve a little problem. i’ve to run a method every 5 minutes with  
cron, but i don’t know what and where i’ve to write it. The method is  
written, but to run it ? i know that i’ve to use the script/runner (does  
it take a lot of resources? does exist something better?), but to run it  
every 5 minutes?

- 5 \* \* \* script/runner -e production “Class.method” ?  
and in which file should i put it ? (i’m under gentoo with vixie-cron)  
Thanks

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/2 "2007-06-09T14:35:56Z")

</div>

On Jun 9, 2007, at 5:13 AM, mike wrote:

> Thanks

I’d create a shell script on your server(s) to do this.

edit ~/scheduled-tasks/my-scheduled-task-name  
##########  
#!/bin/sh

# change directory

export RAILS\_ENV=production  
cd ~/path/to/rails/app  
ruby scipt/runner “Class.method”  
#########

chmod +x ~/scheduled-tasks/my-scheduled-task-name

crontab entry:

- 5 \* \* \* $HOME/scheduled-tasks/my-scheduled-task-name

Something along those lines should work.

Robby

–  
Robby R.  
Founder and Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/3 "2007-06-09T14:36:56Z")

</div>

Hi,

Add following to a file(for e.g. cron\_tasks.txt)

@daily RAILS\_ENV=production /path/to/app/script/runner Controller(or  
Model  
or Mailer).method

Add above file to crontab(cron table) by issuing following command,  
crontab cron\_tasks.txt

For specifics of crontab, look at following,  
[http://www.adminschoice.com/docs/crontab.htm#Crontab%20file](http://www.adminschoice.com/docs/crontab.htm#Crontab%20file)

-Jatinder

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/4 "2007-06-09T14:57:26Z")

</div>

Robby R. wrote:

> I’d create a shell script on your server(s) to do this.
> 
> crontab entry:
> 
> - 5 \* \* \* $HOME/scheduled-tasks/my-scheduled-task-name

Why create an external script?  
This [Crontab – Quick Reference](http://www.adminschoice.com/docs/crontab.htm#Crontab%20file) says  
that the second is for hours:

> A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
> 
> 30 18 \* \* \* rm /home/someuser/tmp/\*

But with something like

5 \* \* \* \* …

it would be run every hour at 5 minutes, like 01:05, 02:05, and so on,  
right ?

with:

- 
  - 
    - 
      - 
        - …

it would be run every minutes…but every 5 minutes? ☹

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/5 "2007-06-09T15:25:00Z")

</div>

On Jun 9, 2007, at 5:57 AM, mike wrote:

> Why create an external script?  
> I prefer an external script so that I can add some documentation if  
> necessary for whomever else might come across this crontab. Also, it  
> allows me to test the script and run it manually when necessary.

We often have special rake tasks and shell scripts that go along with  
our rails applications, and we maintain them in subversion, so, this  
allows us to modify them within svn without needing to re-edit our  
crontab each time there is a change.

> This [Crontab – Quick Reference](http://www.adminschoice.com/docs/crontab.htm#Crontab%20file) says  
> that the second is for hours:

Every five minutes:

0,5,10,15,20,25,30,35,40,45,50,55, \* \* \* \* path/to/script

good luck!

## Robby

Robby R.  
Founder and Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/6 "2007-06-09T15:30:17Z")

</div>

Robby R. wrote:

> I prefer an external script so that I can add some documentation if  
> necessary for whomever else might come across this crontab. Also, it  
> allows me to test the script and run it manually when necessary.
> 
> We often have special rake tasks and shell scripts that go along with  
> our rails applications, and we maintain them in subversion, so, this  
> allows us to modify them within svn without needing to re-edit our  
> crontab each time there is a change.

> Every five minutes:
> 
> 0,5,10,15,20,25,30,35,40,45,50,55, \* \* \* \* path/to/script
> 
> good luck!

ok, i think i’ll use an external script too 🙂  
what about the resources? does it takes a lot (of memory) to start ?

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/7 "2007-06-09T16:06:04Z")

</div>

On Jun 9, 2007, at 6:30 AM, mike wrote:

> > crontab each time there is a change.  
> > ok, i think i’ll use an external script too 🙂  
> > what about the resources? does it takes a lot (of memory) to start ?

It really depends on what your method is doing… You might try  
running it manually and watch your processes in top.

-Robby

–  
Robby R.  
Founder and Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/8 "2007-06-09T16:19:48Z")

</div>

Robby R. wrote:

> It really depends on what your method is doing… You might try  
> running it manually and watch your processes in top.

I’ll try  
Thanks 🙂

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [June 9, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/cron-and-rails/106384/9 "2007-06-09T17:42:18Z")

</div>

Most cron implementations also support this format  
\*/5 \* \* \* \* /path/to/script

Beware that the full rails stack has gotten quite large and loading  
and unloading it from cron can be painful. If you don’t need all of  
rails (for ex. ActionPack), just load what you need. Here’s a quick  
script that loads active record and all the models in a rails project.  
It hasn’t been tested, but should get you most of the way there.

#!/usr/bin/env ruby

require ‘rubygems’  
require ‘active\_record’  
require ‘action\_mailer’

connection = YAML::load(open(’…/config/database.yml’))[“development”]  
connection[“database”] = “…/” + connection[“database”] if  
connection[“adapter”].match(/sqlite/)

ActiveRecord::Base.establish\_connection(connection)

Dir["…/app/models/\*"].each {|m| require m}

puts Link.find(:all).inspect
