Cron and rails

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

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

www.robbyonrails.com

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

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

-Jatinder

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 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? :frowning:

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 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

www.robbyonrails.com

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

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 :slight_smile:
what about the resources? does it takes a lot (of memory) to start ?

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 :slight_smile:
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

www.robbyonrails.com

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

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 :slight_smile:

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