Rails crontab 执行 rake 遇到 cannot load such file -- bundler/setup

fengzhilian818 · February 22, 2017 · Last by fengzhilian818 replied at March 09, 2017 · 1935 hits

在 crontab 添加 0 */1 * * * /xxx/rebuild_index.sh > /www/reindex.log 2>&1 任务并打印日志。

/xxx/rebuild_index.sh的内容为: #! /bin/bash
export LC_ALL=en_US.UTF-8 export PATH='/usr/local/bin':$PATH
cd /www/app RAILS_ENV=production /usr/local/bin/rake sunspot:reindex

日志报错为: rake aborted! LoadError: cannot load such file -- bundler/setup /www/app/config/boot.rb:3:in <top (required)>' /www/app/config/application.rb:1:in' /www/app/Rakefile:4:in `' (See full trace by running task with --trace)

求大神指点一二

木有找到文件啊。。。顺着异常堆栈屡一下,是不是你 require 进来的文件不存在

Bash 的环境和你登陆 SSH 到服务器的环境(环境变量)不同。

所以你最好要在 rebuild_index.sh 里面载入 ~/.bashrc 之类的东西

执行 rake 命令,最好是用 bundle exec rake 来执行,以确保 Gem 环境也能正确加载。


主要错误是这个:

cannot load such file -- bundler/setup

看起来应该是 RubyGems 路径没对应上,执行的 rake 认为 bundler 没有安装。

crontab 的时候,并不会加载当前用户 login 时加载的 profile, rc 类文件,一些工具的路径和初始化是写在这些 script 里面的。于是就出现了 crontab 时找不到的问题。

你的 ruby 是自己编译安装的,还是 rvm or rbenv?

这样试试看

0 */1 * * * /bin/bash -l -c '/xxx/rebuild_index.sh > /www/reindex.log 2>&'

试试在 schedule.rb 里加

env :PATH, ENV['PATH']
env :GEM_PATH, '/home/deploy/.rvm/gems/ruby-2.3.1'

我最后解决了。。。在 rebuild_index.sh 中加入 source /etc/profile

You need to Sign in before reply, if you don't have an account, please Sign up first.