简体   繁体   English

Rails cron 每当,捆绑:找不到命令

[英]Rails cron whenever, bundle: command not found

I am trying to use whenever to execute a rake task onces a day.我正在尝试使用每当每天执行一次 rake 任务。 Im getting this error我收到这个错误

/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.它工作正常,不确定这里发生了什么。

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']

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 -ecrontab -e中的 bundle 命令之前为 ruby​​ 路径的命令添加了新的源代码行

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!!!干杯!!!

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.键入 crontab -e 这将打开 crontab 进行编辑。 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.注释掉以 PATH 开头的两行。

Do the above step whenever you run "bundle exec whenever" command.每当您运行“bundle exec Every”命令时,请执行上述步骤。 And it works.它有效。

No idea why PATH is misleading the environment.不知道为什么 PATH 会误导环境。

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 命令之前。

Forget about PATH settings in cron files.忘记 cron 文件中的 PATH 设置。 Setting the PATH does not work.设置 PATH 不起作用。

Set the path to bundle explicitly in your config/schedule.rb在 config/schedule.rb 中明确设置要捆绑的路径

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

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.我正在使用 Ruby 2.x 和 Rails 4.2 以及 0.9.4 最新版本。 It should work with earlier version as well, if the nature of the issue is same.如果问题的性质相同,它也应该适用于早期版本。

thank you.谢谢你。

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/profile bash file as a setup file.您正在将 bash 命令作为登录 shell 启动,该命令将源(执行) /etc/profile bash 文件作为安装文件。 By doing so, if you check this file, it might have bash command lines that erase your previous $PATH which you do not want to since it contains your path to your bundle and all your other commands in the first place.通过这样做,如果您检查此文件,它可能有 bash 命令行,它会删除您以前不想删除的$PATH ,因为它首先包含您的包路径和所有其他命令。

To fix this issue you just have to remove the lines related to set up the $PATH variable in your /etc/profile file.要解决此问题,您只需删除与在/etc/profile文件中设置$PATH变量相关的行。

I think you should try explicitly setting the GEM_HOME and GEM_PATH environment variables in your crontab.我认为您应该尝试在 crontab 中显式设置 GEM_HOME 和 GEM_PATH 环境变量。 You could also try running something like gem list --local or gem environment through cron and checking the output.您还可以尝试通过 cron 运行gem list --localgem environment并检查输出。

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

This is a ENV['PATH'] not set issue.这是一个 ENV['PATH'] 未设置问题。 The most elegant way to fix this is to append the rvm related scripts to the path right after the install.解决此问题的最优雅方法是在安装后立即将 rvm 相关脚本附加到路径中。 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" ] && return throws error and the subsequent lines are not executed.将以下行添加到 .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 环境变量。

对于使用rbenv您可以使用包含的 shim /home/username/.rbenv/shims/bundle

0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && RAILS_ENV=production /home/af/.rbenv/shims/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'

in 2021, I found a basic solution, just add on top of schedule.rb 2021年,我找到了一个基本的解决方案,只需在schedule.rb之上添加

 env :PATH, ENV['PATH']
 set :output, "log/cron_log.log"
 set :runner_command, "rails runner"

from:从:

https://github.com/javan/whenever/issues/665 https://github.com/javan/whenever/issues/665

I solved this problem by printing out my environmental variables我通过打印我的环境变量解决了这个问题

printenv

finding the ones that look related to Rails.找到看起来与 Rails 相关的那些。 One was a path to gems, the other was GEM_HOME and prepending the command in cron with these two:一个是 gems 的路径,另一个是GEM_HOME并在 cron 中添加以下两个命令:

PATH=$PATH:/home/petr/gems/bin GEM_HOME=/home/petr/gems program_executable

Also in 2021, adding this in schedule.rb worked for me:同样在 2021 年,在 schedule.rb 中添加这个对我有用:

set :job_template, "bash -l -c 'PATH=#{ENV['PATH']} && :job'"

All jobs are by default run with bash -l -c 'command...' ( https://github.com/javan/whenever )默认情况下,所有作业都使用 bash -l -c 'command...' ( https://github.com/javan/whenever ) 运行

So I made bash include ENV['PATH'] in PATH at the beginning and now rails are called from the proper rbenv.所以我在开始时在 PATH 中设置了 bash include ENV['PATH'] ,现在从正确的 rbenv 调用 rails。

对于现代修复,在 capistrano deploy.rb 中添加这一行,

set :whenever_command, "bundle exec whenever"

[root@smbserver current]# crontab -e [root@smbserver 当前]# crontab -e

02 22 * * 1-5 /bin/bash -l -c  /shell/day.sh 
30 14 * * 0 /bin/bash -l -c  /shell/week.sh 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM