2016-08-19 141 views
1

操作系统:Ubuntu安装在Docker上,而Ubuntu安装在我的个人笔记本电脑上。rake命令没有从cron作业运行,但以其他方式运行

我运行这个cron作业:

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

我与束EXEC耙规范试了一下,没有/斌/ bash中,与每当宝石,与在/ usr/local/bin目录/耙规范,但除了这些错误之外,还没有发生任何事

错误日志文件上运行的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它给这个错误。 任何想法?

+1

你用同一个用户运行cron吗?看起来用户cron在doe上运行无法访问所需的gem。 – neongrau

+0

嗨,谢谢你的回答,它以根用户身份运行,操作系统中没有用户..它只有root用户,在Docker中作为容器运行 – user1735921

+0

也许docker是问题的原因吗?尝试为问题添加“docker”标签,并在问题文本中写下它。 – gambala

回答

2

我很确定这个问题是由不正确的环境造成的。基本上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这样的文件。

+0

我在.bashrc中没有rbenv或rvm路径我检查过alll.bashrc – user1735921

+0

但是您使用的是rbenv或rvm,对不对?如果是的话,你应该在环境文件中有相关的代码。没有它,它就无法工作。 – smefju

+0

在/home/myuser/.bashrc中找到它...它的rbenv。但是同样的错误:Bundler :: GemNotFound:在任何源代码中都找不到rake-11.2.2,我安装了两个rake,其中一个是10.something,另一个是11.2.2,两者都在同一个目录中,名为gems。 – user1735921

0

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

+0

已经在使用它,得到相同的错误。 – user1735921