How to run Rails code as a cron job?
Rails comes with a script/runner script that can be used to run a specific snippet of code in your Rails app. There’re many ways to organize your code to let this runner script execute it, but one obvious way is to write the code as a model’s action. Here are quick steps for doing this:
1. Insert your code into an action of a model, i.e. MyModel.DoThisThing, DoThisThing must be defined as class method, “def self.DoThisThing…”
2. In your cron job, add a line to run this at certain interval:
/usr/local/bin/ruby /path/to/your/app/script/runner -e production "MyModel.DoThisThing"
Change the ruby path on your system if different.