为什么 rake 任务不通过 cron 运行?
Why does rake task not run via cron?
我已经定义了一个任务,使得 运行ning rake background:fetch_image
作为我的用户按预期工作。如果我 运行 sudo -u www-data rake background:fetch_image
,它也会按预期工作。我不相信我要描述的错误是由我在任务本身中编写的代码引起的。
我正在使用 whenever gem 来处理日程安排。在设置一些变量以更正以前的错误后 每当 在 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'
当 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'
只有当任务以这种方式通过 cron 运行 时才会发生此错误。
我尝试包括扩展 PATH
变量并执行 rbenv init
的解决方案,但 none 更正了这个问题。
为什么 cron 的行为如此不同以致导致此错误?如何解决?
我最终更改了使用的 PATH 变量,以便使用 /usr/local/bin/ruby 而不是 /usr/bin/ruby。我通过将 here 中找到的代码修改为 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}
这使 cron 和我的用户使用的版本一致。
我已经定义了一个任务,使得 运行ning rake background:fetch_image
作为我的用户按预期工作。如果我 运行 sudo -u www-data rake background:fetch_image
,它也会按预期工作。我不相信我要描述的错误是由我在任务本身中编写的代码引起的。
我正在使用 whenever gem 来处理日程安排。在设置一些变量以更正以前的错误后 每当 在 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'
当 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'
只有当任务以这种方式通过 cron 运行 时才会发生此错误。
我尝试包括扩展 PATH
变量并执行 rbenv init
的解决方案,但 none 更正了这个问题。
为什么 cron 的行为如此不同以致导致此错误?如何解决?
我最终更改了使用的 PATH 变量,以便使用 /usr/local/bin/ruby 而不是 /usr/bin/ruby。我通过将 here 中找到的代码修改为 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}
这使 cron 和我的用户使用的版本一致。