Ruby Can';不能让rails rake任务与crontab配合使用

Ruby Can';不能让rails rake任务与crontab配合使用,ruby,ruby-on-rails-3,rake,crontab,Ruby,Ruby On Rails 3,Rake,Crontab,我目前有这个shell脚本 每晚 #!/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 我在这里的crontab条目中使用它crontab-e 42 20 * * * /home/appname/night

我目前有这个shell脚本

每晚

#!/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
我在这里的crontab条目中使用它<代码>crontab-e

42 20 * * * /home/appname/nightly.sh
当它运行时,我得到这个错误

/home/appname/nightly.sh: line 4: bundle: command not found
我正在使用RVM

我现在已经根据@KL-7向我的crontab添加了一些环境变量

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
现在我明白了

/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>'
/home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site\u ruby/1.9.1/rubygems/dependency.rb:247:在“to_specs”中:找不到绑定器
[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\u ruby/1.9.1/rubygems/dependency.rb:256:in'to_spec'
from/home/appname/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site\u ruby/1.9.1/rubygems.rb:1210:in'gem'
from/home/appname/.rvm/gems/ruby-1.9.2-p290/bin/bundle:18:in`'

这可能是因为它抛出了一个错误,而您没有捕获它。请尝试以下操作:

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

似乎
cron
找不到您的
bundle
可执行文件。您需要找到它(例如,使用
which bundle
),然后在crontab中指定它的完整路径,或者在crontab的顶部设置
path
环境变量,如下所示:

PATH=/bin:/usr/bin:/path/to/directory/with/bundle/
这可能有助于: /bin/bash-l-c

阅读:
这里是另一个解决方案:

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

这将只是ssh连接到同一个主机(正常环境的来源)并发出命令

这将做什么?抱歉,请不要养成在不理解的情况下复制/粘贴到术语中的习惯。
2>&1
将进程的stderr重定向到其stdout。请参阅,例如,了解更多信息。第一个重定向是针对stdout的;第二个是stderr。您捕获的是stdout,而不是stderr。如果您可以从命令行运行它而不出错,请确保在命令中使用正确的PATH变量。理想情况下,创建一个包含所有命令的shell脚本,并从cron执行该shell脚本。它是否从命令行工作?如果是这样,它也应该从脚本中工作。但是,您需要导出变量,以便子shell也可以访问它。有效地为每个变量“export PATH=blah:blah”。您是手动创建这个crontab条目,还是使用where或smth之类的工具创建的?@FrederickCheung Yes!谢谢你的提问,可能是一个重要的问题,嗯?你的rails应用程序有Rakefile吗?好的,但是
PATH
env变量没有改变。尝试将路径添加到带有
bundle
可执行文件的目录,或者至少在脚本中指定
bundle
的完整路径。按照建议再次更新。