Ruby-on-rails Rails cron 每当,捆绑:找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9482298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:56:19  来源:igfitidea点击:

Rails cron whenever, bundle: command not found

ruby-on-railscronwhenever

提问by Josh Crowder

I am trying to use whenever to execute a rake task onces a day. Im getting this error

我正在尝试使用每当每天执行一次 rake 任务。我收到这个错误

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

Here is my crontab

这是我的 crontab

# Begin Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb
PATH=/home/af/.rvm/gems/ruby-1.9.2-p180@global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'

# End Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb

I'm at a loss as to why it isn't working. If I run the command:

我不知道为什么它不起作用。如果我运行命令:

cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1

It works fine, not sure whats going on here.

它工作正常,不确定这里发生了什么。

采纳答案by Josh Crowder

I played around with this all afternoon and couldn't find a better solution. Here is what I have come up with

我整个下午都在玩这个,找不到更好的解决方案。这是我想出的

bundle install --binstubs

and then run

然后运行

bin/rake daily:stats

回答by Dennis Kuczynski

You can also ensure your PATH ends up in the crontab, by putting the following at the top of the schedule.rb file:

您还可以通过将以下内容放在 schedule.rb 文件的顶部来确保您的 PATH 最终出现在 crontab 中:

env :PATH, ENV['PATH']

https://groups.google.com/forum/#!msg/whenever-gem/yRLt3f2jrfU/Exu3xfCo8DAJ

https://groups.google.com/forum/#!msg/whenever-gem/yRLt3f2jrfU/Exu3xfCo8DAJ

If above solution don't work for you, try:

如果上述解决方案对您不起作用,请尝试:

env :GEM_PATH, ENV['GEM_PATH']

回答by buddy

After so many try outs the following seems to work

经过如此多的尝试,以下似乎有效

Type the following from terminal

从终端输入以下内容

  1. Type crontab -e This opens the crontab for editing. You will see two lines as below:

    # cron clears out environment variables, but Rubber.root/script/rubber uses
    # "rvm do default" to run, so no longer any need to setup ruby env vars here,
    # all we need is PATH
    PATH=/<path to bundle>/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
    

    AND

    # Begin Whenever generated tasks for: /mnt/wamjoke-production/releases/20120912$
    PATH=/<path to bundle>/shared/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
    
  2. Comment out both lines beginning with PATH.

  1. 键入 crontab -e 这将打开 crontab 进行编辑。您将看到如下两行:

    # cron clears out environment variables, but Rubber.root/script/rubber uses
    # "rvm do default" to run, so no longer any need to setup ruby env vars here,
    # all we need is PATH
    PATH=/<path to bundle>/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
    

    # Begin Whenever generated tasks for: /mnt/wamjoke-production/releases/20120912$
    PATH=/<path to bundle>/shared/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
    
  2. 注释掉以 PATH 开头的两行。

Do the above step whenever you run "bundle exec whenever" command. And it works.

每当您运行“bundle exec Every”命令时,请执行上述步骤。它有效。

No idea why PATH is misleading the environment.

不知道为什么 PATH 会误导环境。

回答by Sumit Munot

In my case I just ran :

就我而言,我只是跑了:

rvm env --path -- ruby-version[@gemset-name]

Referring to cron job setup doc

参考cron 作业设置文档

Added new source line to the command for ruby path before bundle command in the crontab -e

在命令中的 bundle 命令之前为 ruby​​ 路径的命令添加了新的源代码行 crontab -e

source /usr/local/rvm/environments/ruby-1.9.3-p392;

Now the commands like as below:

现在命令如下:

Before:

前:

0 4 * * * cd /home/current && bundle exec rake my_rake RAILS_ENV=production

After:

后:

0 4 * * * cd /home/current && source /usr/local/rvm/environments/ruby-1.9.3-p392; bundle exec rake my_rake RAILS_ENV=production

Cheers!!!

干杯!!!

回答by cailinanne

I hate this problem - I've spent hours trying to solve it too.

我讨厌这个问题 - 我也花了几个小时试图解决它。

What works for me is to add

对我有用的是添加

RAILS_ENV=production; source /usr/local/rvm/scripts/rvm;

before the bundle command.

在 bundle 命令之前。

回答by vanboom

Forget about PATH settings in cron files. Setting the PATH does not work.

忘记 cron 文件中的 PATH 设置。设置 PATH 不起作用。

Set the path to bundle explicitly in your config/schedule.rb

在 config/schedule.rb 中明确设置要捆绑的路径

set :bundle_command, "/usr/local/bin/bundle"

set :bundle_command, "/usr/local/bin/bundle"

回答by Sebi

I think you should try explicitly setting the GEM_HOME and GEM_PATH environment variables in your crontab. You could also try running something like gem list --localor gem environmentthrough cron and checking the output.

我认为您应该尝试在 crontab 中显式设置 GEM_HOME 和 GEM_PATH 环境变量。您也可以尝试运行类似gem list --localgem environment通过 cron 的程序并检查输出。

回答by Rameshwar Vyevhare

You can try below solution which I found while googling and that works for me finally....hope that should work with you.

您可以尝试以下我在谷歌搜索时找到的解决方案,最终对我有用....希望对您有用。

I implemented and tested the same on production make sure that to change environment accordingly -

我在生产中实施并测试了相同的内容,以确保相应地改变环境 -

set :output, "{your path on the server}/log/cron_log.log"
 set :environment, :production
 env :PATH, ENV['PATH']
 job_type :rbenv_rake, %q!eval "$(rbenv init -)"; cd :path && :environment_variable=:environment bundle exec rake :task --silent :output!

Best luck, This issue occurred after 3 years as I was using before just simple what given on the gem documentation on production.

祝你好运,这个问题发生在 3 年后,因为我在使用之前只是简单的关于生产的 gem 文档中给出的内容。

I'm using Ruby 2.x and Rails 4.2 with whenever 0.9.4 latest version. It should work with earlier version as well, if the nature of the issue is same.

我正在使用 Ruby 2.x 和 Rails 4.2 以及 0.9.4 最新版本。如果问题的性质相同,它也应该适用于早期版本。

thank you.

谢谢你。

回答by axzwl

By executing a command that way: /bin/bash -l -c

通过以这种方式执行命令: /bin/bash -l -c

You are launching a bash command as a login shell which is going to source (execute) /etc/profilebash file as a setup file. By doing so, if you check this file, it might have bash command lines that erase your previous $PATHwhich you do not want to since it contains your path to your bundle and all your other commands in the first place.

您正在将 bash 命令作为登录 shell 启动,该命令将源(执行)/etc/profilebash 文件作为安装文件。通过这样做,如果您检查此文件,它可能具有 bash 命令行,这些命令行会删除您$PATH不想删除的前一个,因为它首先包含您的包路径和所有其他命令。

To fix this issue you just have to remove the lines related to set up the $PATHvariable in your /etc/profilefile.

要解决此问题,您只需删除与$PATH/etc/profile文件中设置变量相关的行。

回答by Lavixu

This is a ENV['PATH'] not set issue. The most elegant way to fix this is to append the rvm related scripts to the path right after the install. Add the following lines to beginning of .bashrc ( beginning and not end as when .bashrc is accessed by a non-interactive shell, the line [ -z "$PS1" ] && returnthrows error and the subsequent lines are not executed.

这是一个 ENV['PATH'] 未设置问题。解决此问题的最优雅方法是在安装后立即将 rvm 相关脚本附加到路径中。将以下行添加到 .bashrc 的开头(开始而不是结束,因为当 .bashrc 被非交互式 shell 访问时,该行[ -z "$PS1" ] && return抛出错误并且不执行后续行。

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

and not try to explicitly set PATH and sully environment variables.

并且不要尝试显式设置 PATH 和 sully 环境变量。