質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
cron

cronは、Unix系OS上でデーモンプロセスとして動作する、スクリプトの自動実行が可能なジョブスケジューラです。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

sh

shは、UNIX系OSのシェル操作の1つであり、最も基本的なシェルのことです。

Q&A

0回答

583閲覧

railsでcronを実行した時、bundleとgemの読み込みでエラーが発生してしまう。

Daimian

総合スコア53

cron

cronは、Unix系OS上でデーモンプロセスとして動作する、スクリプトの自動実行が可能なジョブスケジューラです。

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

sh

shは、UNIX系OSのシェル操作の1つであり、最も基本的なシェルのことです。

0グッド

1クリップ

投稿2019/02/22 07:18

編集2019/02/22 14:22

前提・実現したいこと

下記の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から実行させると上記のようなエラーが出てしまうのでしょうか・・・

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Stan_Dma

2019/02/22 07:37 編集

Sorry i was wrong. -- I dont have a good clue but it seems that you should move to the directory in which your rails project exists within your cron execution. --
Daimian

2019/02/22 08:39

thank you for commenting! in the /app/kaminari_API/kaminari_API_rails/lib/tasks/dl_and_save_lightning.sh, ``` #!/bin/sh cd /app/kaminari_API/kaminari_API_rails ``` My intention was to move to the directory in which your rails project exists. I think it works. so what is the other problem...
t_obara

2019/02/22 08:47

cronでの大きな違いとして考えられるのは環境変数の違いです。環境変数を確認してみてはいかがでしょうか。
Daimian

2019/02/22 09:45

PATH=/app/kaminari_API/kaminari_API_rails/vendor/bundle/ruby/2.3.0/bin:/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin BUNDLER_ORIG_PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin BUNDLE_BIN_PATH=/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/exe/bundle BUNDLE_GEMFILE=/app/kaminari_API/kaminari_API_rails/Gemfile RUBYOPT=-rbundler/setup RUBYLIB=/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib 環境変数だとこの辺が違うみたいです!ちょっとそれがどう関わってくるのかは今調査してみてます。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問