1

In my Rails 3.2 app I have a custom rake task that I am trying to run every day at 5pm with a cron job. Right now I am running it on our site's Staging server. The cron job is setup correctly but according to the e-mail output from the cron daemon the rake is being aborted because it's trying to invoke sqlite3.

Here is my cron job:

#crontab
0 17 * * * cd /u/apps/my_app/current && /usr/local/bin/rake my_task

I have reserved sqlite3 for development and test, like so:

#Gemfile
group :development, :test do
  gem 'factory_girl_rails'                
  gem 'letter_opener'                      
  gem 'rspec-rails'            
  gem 'sqlite3'                             
  gem 'thin'
  gem 'pry-rails'                              
end

I also have set my rake task to load the proper environment like so:

#mytask.rake
task :my_task => :environment do
    # my task
end

This is the error I'm getting in the e-mail from crond:

rake aborted!
Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.)

If I run the rake tasks directly from the shell I don't get an error. It would seem that this works because of the following line in ~/.bashrc:

alias rake='RAILS_ENV=staging rake'

This, however, doesn't seem to have any effect on the task when run from the cron job. I've tried adding export RAILS_ENV=staging to the .bashrc file as recommended here but it didn't help.

The one thing I've found that works is writing the cron job like this:

#crontab 
0 17 * * * cd /u/apps/my_app/current && /usr/local/bin/rake my_task RAILS_ENV=staging

...with the env declaration directly in the cron command. This doesn't seem very elegant but it's okay for now. Is there a better way to go about this?

Community
  • 1
  • 1
sixty4bit
  • 7,422
  • 7
  • 33
  • 57
  • how about `0 17 * * * cd /u/apps/my_app/current && export RAILS_ENV=stage /usr/local/bin/rake my_task`? – ptierno Aug 09 '14 at 21:33
  • My current solution is the same, just with the env declaration at the end of the second command of the cron job. I was hoping to find a way to set it up so I don't need to declare it for every cron job task. – sixty4bit Aug 10 '14 at 21:36
  • Is `sqlite3` available on your staging part in your Gemfile? If you run `RAILS_ENV=staging rake my_task` does it succeed? (I think not). Also, are you sure that you have limited SQLite for dev and test only? Are your config files set up for that? – xlembouras Aug 12 '14 at 05:42
  • @xlembouras If I run the rake tasks directly from the shell I don't get an error. Adding "RAILS_ENV=staging" to the rake task in `crontab -e` also fixes the problem but I'm looking for a way to not have to declare that in every cron job. – sixty4bit Aug 12 '14 at 14:50

1 Answers1

0

Υou can set the environment variables in crontab (not in Arch or RedHat though...)

try setting your crontab as follows

#crontab 

RAILS_ENV=staging

0 17 * * * cd /u/apps/my_app/current && /usr/local/bin/rake my_task
xlembouras
  • 8,215
  • 4
  • 33
  • 42