# Background process (cron job) and business rules + database

**URL:** <https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895>\
**Category:** Rails\
**Created:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895 "2006-11-07T11:00:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895/1 "2006-11-07T11:00:15Z")

</div>

My application needs to send email reminders to people before certain  
events. I think cron job is the way to go which will run every hour.

Rather than putting the database connection and all the business rules  
in  
the shell script , I was wondering if there is a better way. I haven’t  
used  
rake but is it possible to write a rake job and execute it through a  
cron  
job. Any suggestions on handling these kinds of issues.

Thanks.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895/2 "2006-11-07T11:00:16Z")

</div>

> My application needs to send email reminders to people before certain  
> events. I think cron job is the way to go which will run every hour.
> 
> Rather than putting the database connection and all the business rules in  
> the shell script , I was wondering if there is a better way. I haven’t used  
> rake but is it possible to write a rake job and execute it through a cron  
> job. Any suggestions on handling these kinds of issues.

Define a method in the appropriate model and then call it like this from  
cron:

/path/to/rails\_root/script/runner “MyModel.my\_method(arg, arg)”

Then you’ll have access to your db connection just like you would  
normally  
as well as all your other models, etc.

-philip

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895/3 "2006-11-07T11:00:39Z")

</div>

On 11/4/06, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> cron:
> 
> /path/to/rails\_root/script/runner “MyModel.my\_method(arg, arg)”
> 
> Then you’ll have access to your db connection just like you would normally  
> as well as all your other models, etc.
> 
> -philip
> 
> >

You are forgetting backgroundrb!!!

## gnufied

There was only one Road; that it was like a great river: its springs  
were at every doorstep, and every path was its tributary.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 7, 2006, 11:00am UTC](https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895/4 "2006-11-07T11:00:41Z")

</div>

Thanks guys.

I didn’t know about script/runner.

I also missed out on backgrounddrb.

-=-

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [November 7, 2006, 11:01am UTC](https://www.ruby-forum.com/t/background-process-cron-job-and-business-rules-database/74895/5 "2006-11-07T11:01:02Z")

</div>

On Nov 4, 2006, at 3:59, Neeraj K. wrote:

> Rather than putting the database connection and all the business  
> rules in the shell script , I was wondering if there is a better  
> way. I haven’t used rake but is it possible to write a rake job and  
> execute it through a cron job. Any suggestions on handling these  
> kinds of issues.

It sure is possible to run a rake command from cron. However, you can  
also use script/runner for this:

./script/runner “your code goes here”

So for example script/runner “require ‘send\_reminders’” or script/  
runner ‘Reminder.deliver\_all’ can be run from your cronjob.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)
