Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake task and cron in rails 3 with rvm

I am trying to run a Rake task using crontab in Rails 3.0.9(using RVM), But it is not working.

But when i running in the console it works fine

My Rake Task

namespace :alert  do
  desc "create some reminder notification"
  task :send_reminder => :environment do
     p "my task goes here ----"
  end
end

My cron task

*/1 * * * * cd /home/anu-karthik/Documents/billguru/ && /home/anu-karthik/.rvm/gems/ruby-1.9.2-p0/bin/rake  alert:send_reminder >/home/anu-karthik/alert.out

But I didn't found any log entry in "alert.out" file

also I have tried with following method

*/1 * * * * cd /home/anu-karthik/Documents/billguru/ && rake  alert:send_reminder >/home/anu-karthik/alert.out

now output is (in /home/anu-karthik/Documents/billguru)

I think it is the problem with RVM. How do I solve this issue? Thanks in advance

like image 751
Karthi Ponnusamy Avatar asked Apr 07 '12 08:04

Karthi Ponnusamy


1 Answers

According to RVM documentation: https://rvm.io/integration/cron

For every rvm or gemset there is an environment file which describes it. You can obtain it with:

rvm env --path -- ruby-version[@gemset-name]

That is the path to the environment file of rvm.

It is a good habit when running rake tasks from cron, to invoke them from a shell script.

The crontab should be something like:

*/30 * * * * /path/to/shell/script.sh >/dev/null 2>&1

And then in the shell script :

#!/bin/bash
cd /path/to/project
source /path/to/env/file
rake task

Hope this helps!

like image 103
Tania R Avatar answered Oct 02 '22 14:10

Tania R