【问题标题】:rake command not running from cron job but works otherwiserake 命令未从 cron 作业运行,但可以正常工作
【发布时间】:2019-03-23 00:48:03
【问题描述】:

操作系统:Ubuntu 安装在 docker 上,普通 ubuntu 安装在我的个人笔记本电脑上。

我正在运行这个 cron 作业:

*/5 * * * * /bin/bash -l -c "cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"

我已经尝试过使用 bundle exec rake 规范,没有 /bin/bash,使用任何 gem,使用 /usr/local/bin/rake 规范,但除了这些错误之外什么都没有发生。

运行 cron 作业时日志文件中的错误:

/bin/bash: bundle: command not found


/bin/bash: rake: command not found


rake aborted!
Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
/home/deploy/railsapp/config/boot.rb:3:in `<top (required)>'
/home/deploy/railsapp/config/application.rb:1:in `<top (required)>'
/home/deploy/railsapp/Rakefile:4:in `<top (required)>'
LoadError: cannot load such file -- bundler/setup
/home/deploy/railsapp/config/boot.rb:3:in `<top (required)>'
/home/deploy/railsapp/config/application.rb:1:in `<top (required)>'
/home/deploy/railsapp/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)


    bundler: failed to load command: rake (/usr/local/bin/rake)
        Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:95:in `block in materialize'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `map!'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `materialize'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:140:in `specs'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:185:in `specs_for'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:174:in `requested_specs'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/environment.rb:19:in `requested_specs'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:14:in `setup'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:95:in `setup'
          /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/setup.rb:19:in `<top (required)>'
          /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
          /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

从命令行手动运行rake spec 时它工作正常,但从 cron 中它给出了这个错误。 有什么想法吗?

【问题讨论】:

  • 您是否使用同一用户运行 cron?看起来正在运行的用户 cron 无权访问所需的 gem。
  • 您好,感谢您的回答,它以root身份运行,操作系统中没有用户..它只有root用户,作为docker内的容器运行
  • 也许 docker 是问题的原因?尝试为问题添加docker标签并在问题文本中写下它。
  • 它在我的本地机器上给出了同样的错误,所以 docker 与它无关。

标签: ruby-on-rails cron rake


【解决方案1】:

我很确定问题是由于环境设置不正确引起的。基本上 cron 在最小环境下运行,因此它不知道来自例如 .bashrc 文件的设置(您有 rvmrbenv 初始化和 PATH 设置)。

使用. 获取环境文件,因此将cron 条目重写为:

*/5 * * * * /bin/bash -l -c ". /etc/environment && cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"

请注意,/etc/environment 只是一个示例。您可以改为使用 .bashrc 之类的源文件。

【讨论】:

  • 我在 .bashrc 中没有 rbenv 或 rvm 路径我已经检查了所有的 .bashrc
  • 但是您使用的是 rbenv 或 rvm,对吗?如果是,您应该在环境文件中的某处有相关代码。没有它就行不通。
  • 在 /home/myuser/.bashrc ...它的 rbenv 中找到它。但仍然出现同样的错误:Bundler::GemNotFound: 在任何来源中都找不到 rake-11.2.2,我安装了两个 rake,一个是 10.something,一个是 11.2.2,两者都在同一个名为 gems 的目录中。
  • 所以仍然没有正确设置 PATH 或 GEM_PATH
  • 这完全取决于您的环境。 Afaik 运行 rvm 初始化应该足够了,但我可能错了。您可以在 cron 中运行捆绑检查,重定向输出并从文件中查看。
【解决方案2】:

我使用whenever gem 来管理 crontab。

添加

ENV.each { |k, v| env(k, v) }

config/schedule.rb 文件为我解决了这个问题。

参考:https://github.com/javan/whenever/issues/656#issuecomment-239111064

【讨论】:

    【解决方案3】:

    使用whenever gem - 这是 cron 的适配器,支持 rake 任务和其他 ruby​​/rails 东西。

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 2011-10-19
      • 2017-03-09
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      相关资源
      最近更新 更多