[英]Why does rake task not run via cron?
I have defined a task such that running rake background:fetch_image
as my user works as expected.我已经定义了一个任务,以便我的用户按预期运行
rake background:fetch_image
。 It also works as expected if I run sudo -u www-data rake background:fetch_image
.如果我运行
sudo -u www-data rake background:fetch_image
它也会按预期工作。 I do not believe the error I shall describe is caused by code I wrote in the task itself.我不相信我将描述的错误是由我在任务本身中编写的代码引起的。
I am using the whenever gem to handle scheduling.我正在使用每当gem 来处理调度。 After setting some variables to correct previous errors whenever is producing a line in the crontab like:
在设置一些变量以纠正以前的错误后,无论何时在 crontab 中生成一行,例如:
0 2 * * * /bin/bash -l -c 'cd /var/www/site && RAILS_ENV=production /usr/local/bin/bundle exec rake background:fetch_image --silent >> /var/www/site/log/whenever.log 2>&1'
When cron executes this I get this error:当 cron 执行此操作时,我收到此错误:
/usr/local/lib/ruby/2.6.0/pathname.rb:43:in `chop_basename': undefined method `match?' for /\A(?-mix:\/)?\z/:Regexp (NoMethodError)
Did you mean? match
from /usr/local/lib/ruby/2.6.0/pathname.rb:359:in `plus'
from /usr/local/lib/ruby/2.6.0/pathname.rb:351:in `+'
from /usr/local/lib/ruby/2.6.0/pathname.rb:188:in `parent'
from /usr/local/lib/ruby/2.6.0/bundler/shared_helpers.rb:29:in `root'
from /usr/local/lib/ruby/2.6.0/bundler.rb:234:in `root'
from /usr/local/lib/ruby/2.6.0/bundler.rb:246:in `app_config_path'
from /usr/local/lib/ruby/2.6.0/bundler.rb:273:in `settings'
from /usr/local/lib/ruby/2.6.0/bundler.rb:84:in `configured_bundle_path'
from /usr/local/lib/ruby/2.6.0/bundler.rb:351:in `use_system_gems?'
from /usr/local/lib/ruby/2.6.0/bundler.rb:541:in `configure_gem_path'
from /usr/local/lib/ruby/2.6.0/bundler.rb:534:in `configure_gem_home_and_path'
from /usr/local/lib/ruby/2.6.0/bundler.rb:66:in `configure'
from /usr/local/lib/ruby/2.6.0/bundler.rb:134:in `definition'
from /usr/local/lib/ruby/2.6.0/bundler.rb:101:in `setup'
from /usr/local/lib/ruby/2.6.0/bundler/setup.rb:20:in `<top (required)>'
from /usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /usr/local/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
This error only occurs when the task is run via cron in this manner.仅当以这种方式通过 cron 运行任务时才会发生此错误。 I have tried including solutions that extend the
PATH
variable and perform an rbenv init
but none corrected this issue.我尝试包含扩展
PATH
变量并执行rbenv init
解决方案,但没有解决此问题。
Why is it that cron behaves so differently as to cause this error and how might it be fixed?为什么 cron 的行为如此不同以致导致此错误以及如何修复?
I ended up changing the PATH variable used so that /usr/local/bin/ruby was used rather than /usr/bin/ruby.我最终更改了使用的 PATH 变量,以便使用 /usr/local/bin/ruby 而不是 /usr/bin/ruby。 I did it by modifying the code found here to this result in
schedule.rb
:我通过将此处找到的代码修改为
schedule.rb
结果来做到这一点:
job_type :rake, %Q{export PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin; cd :path && :environment_variable=:environment bundle exec rake :task --silent :output}
This brought the versions being used by cron and my users in line.这使 cron 和我的用户使用的版本一致。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.