Rails3, Running rake task from cron

2019-07-07 02:01发布

问题:

I'm having problems running my rake task from cron, I wrap it in shell file and when I execute this shell file from console it works fine.

#!/bin/sh

if ps -ef | grep -v grep | grep create_all_accounts ; then
    exit 0
else
    cd /home/prosoftstudio/www/prosoftstudio_egabinet && /home/prosoftstudio/www/.ruby/gems/1.8/bin/rake gabinet:create_all_accounts RAILS_ENV=production --trace
    exit 0
fi

Entry in crontab looks like this(I set PATH and GEM_PATH)

PATH=/home/prosoftstudio/www/.python/bin:/usr/local/python2.6/bin:/home/prosoftstudio/www/.ruby/gems/1.8/bin/:/usr/local/ruby1.8/bin:/usr/local/bin:/usr/bin:/bin:/us$
GEM_PATH=/home/prosoftstudio/www/.ruby/gems/1.8:/home/prosoftstudio/www/.ruby/gems/1.8/bundler/gems:/usr/lib/ruby/gems/1.8/
*/1 * * * * /home/prosoftstudio/www/cron_create_accounts.sh > cron_log.txt 2>&1

The output I get is

rake aborted!
git://github.com/100hz/rails-settings.git (at master) is not checked out. Please run `bundle install`

It seems like it can't find gems installed with

gem "rails-settings", :git => "git://github.com/100hz/rails-settings.git"

Anyone know how to fix this?

回答1:

I came up with workaround - installing rails-settings from source

wget https://github.com/100hz/rails-settings/zipball/master --no-check-certificate
unzip 100hz-rails-settings-v0.1.1-0-g330b958.zip
cd 100hz-rails-settings-330b958/
gem build rails-settings.gemspec
gem install rails-settings-0.1.1.gem

and you have to remove ":git =>" from gem "rails-settings" in Gemfile, and run

bundle install

to update Gemfile.lock

After that my script run from cron.



回答2:

To avoid building a gem, another option is to put the gem content in the vendor folder and reference it through :path in Gemfile:

gem "my_gem", :path => "vendor/my_gem"