jondavidjohn
jondavidjohn

Reputation: 62392

Can't get rails rake task to play nice with crontab

I currently have this shell script ...

nightly.sh

#!/bin/bash
rvm 1.9.2
cd /home/appname/capistrano/current
RAILS_ENV=production bundle exec rake nightly >> /home/appname/capistrano/shared/log/nightly.log 2>&1

I use it in my crontab entry here... crontab -e

42 20 * * * /home/appname/nightly.sh

When it runs I get this error

/home/appname/nightly.sh: line 4: bundle: command not found

I am using RVM

I've now Added some environmental variables to my crontab per @KL-7

SHELL=/bin/bash
HOME=/home/appname
PATH=/home/appname/local/bin:/home/appname/.rvm/gems/ruby-1.9.2-p290/bin:/home/appname/.rvm/gems/ruby-1.9.2-p290@global/bin:/home/appname/.rvm/rubies/ruby-1.9.2-p290/bin:/home/appname/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Now I'm getting this...

/home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler
[minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
        from /home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
        from /home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
        from /home/appname/.rvm/gems/ruby-1.9.2-p290/bin/bundle:18:in `<main>'

Upvotes: 2

Views: 3985

Answers (4)

weexpectedTHIS
weexpectedTHIS

Reputation: 3376

Here is another solution:

* * * * * ssh localhost 'your command here...'

This will just ssh in to the same host (which sources the normal environment) and issues the command

Upvotes: -1

dlh
dlh

Reputation: 21

This might help: /bin/bash -l -c

Read: http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production

Upvotes: 2

KL-7
KL-7

Reputation: 47588

Seems like cron can't locate your bundle executable. You need to find it (using, for example, which bundle) and then either specify full path to it in crontab or set PATH environment variable at the top of crontab like that:

PATH=/bin:/usr/bin:/path/to/directory/with/bundle/

Upvotes: 2

souser
souser

Reputation: 6100

It could be because its throwing an error and you are not capturing it. Try the following:

01 04 * * * /bin/bash -l -c 'cd /home/appname/capistrano/current && RAILS_ENV=production bundle exec rake nightly' >> /home/appname/capistrano/shared/log/nightly.log 2>&1

Upvotes: 3

Related Questions