前提・実現したいこと
下記のshファイルに記載されたシェルスクリプトを実行し、記載した処理を実行したいです。
発生している問題・エラーメッセージ
cronでshファイルを実行させていますが、出力先であるログを確認すると下記のようなエラーメッセージが記載されてます。。
Fri Feb 22 06:52:01 UTC 2019: executed Lightning.download_nc_file_from_NASA bundler: failed to load command: /usr/local/bundle/bin/rails (/usr/local/bundle/bin/rails) Bundler::GemNotFound: Your bundle is locked to rake (12.3.2), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of rake (12.3.2) has removed it. You'll need to update your bundle to a different version of rake (12.3.2) that hasn't been removed in order to install. /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:179:in `rescue in specs' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:173:in `specs' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:233:in `specs_for' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:222:in `requested_specs' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:118:in `block in definition_method' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:19:in `setup' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:99:in `setup' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/setup.rb:20: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' Fri Feb 22 06:53:01 UTC 2019: executed Lightning.read_csv_and_write_db bundler: failed to load command: /usr/local/bundle/bin/rails (/usr/local/bundle/bin/rails) Bundler::GemNotFound: Your bundle is locked to rake (12.3.2), but that version could not be found in any of the sources listed in your Gemfile. If you haven't changed sources, that means the author of rake (12.3.2) has removed it. You'll need to update your bundle to a different version of rake (12.3.2) that hasn't been removed in order to install. /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:179:in `rescue in specs' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:173:in `specs' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:233:in `specs_for' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/definition.rb:222:in `requested_specs' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:118:in `block in definition_method' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:19:in `setup' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:99:in `setup' /usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/setup.rb:20: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'
該当のソースコード
/app/kaminari_API/kaminari_API_rails/lib/tasks/dl_and_save_lightning.sh
#!/bin/sh cd /app/kaminari_API/kaminari_API_rails echo "$(date): executed Lightning.download_nc_file_from_NASA" >> /var/log/cron.log 2>&1 /usr/local/bin/bundle exec /usr/local/bundle/bin/rails runner -e development Lightning.download_nc_file_from_NASA >> /var/log/cron.log 2>&1 sleep 60s echo "$(date): executed Lightning.read_csv_and_write_db" >> /var/log/cron.log 2>&1 /usr/local/bin/bundle exec /usr/local/bundle/bin/rails runner -e development Lightning.read_csv_and_write_db >> /var/log/cron.log 2>&1
/etc/cron.d/api_cron
*/2 * * * * root /app/kaminari_API/kaminari_API_rails/lib/tasks/dl_and_save_lightning.sh
試したこと
通常のrailsアプリ配下 /app/kaminari_API/kaminari_API_rails/のディレクトリ位置で bundleとrailsの実行ファイルのパスを確認すると、
root@27ec0cfd9457:/app/kaminari_API/kaminari_API_rails# which bundle /usr/local/bin/bundle root@27ec0cfd9457:/app/kaminari_API/kaminari_API_rails# which rails /usr/local/bundle/bin/rails
となるため、パス指定は間違ってないと思います。さらに、/app/kaminari_API/kaminari_API_railsで
rails cを打っても、
root@27ec0cfd9457:/app/kaminari_API/kaminari_API_rails# bundle exec rails c Loading development environment (Rails 5.2.2) [1] pry(main)>
とちゃんと起動します。なぜcronで実施した時のみ、エラーが出るのでしょうか。。
その後、色々試しています。
sh /app/kaminari_API/kaminari_API_rails/lib/tasks/dl_and_save_lightning.sh
でcronからではなく、ターミナルから直接コマンドを実行すると、ちゃんと動作してくれます。なぜcronから実行させると上記のようなエラーが出てしまうのでしょうか・・・
あなたの回答
tips
プレビュー