LoginSignup
0
0

More than 1 year has passed since last update.

rails g modelをしたら「FATAL: Listen error...」が出たので、その時の対処法

Last updated at Posted at 2021-07-14

今回初投稿という事もあり、色々と不慣れで見にくいとは思いますが、暖かい目で見てください。

タイトル通り、今回rails g modelをした所「FATAL: Listen error: unable to monitor directories for changes.」というエラーが発生したため備忘録として残します。

現在のrails環境のバージョン

Ruby on Rails バージョン: 6.1.4
vagrant 2.2.17

エラーした時の状況
$ rails g model Hogehoge hoge:hoge hoge:hoge

をした所

エラー文
FATAL: Listen error: unable to monitor directories for changes.
Visit https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers for info on how to fix this.

というのが発生しました。

google先生で訳し、簡潔で言うと
・ディレクトリの変更を監視することができません。
・なので記載した、サイトにアクセスして、このエラーを修正してください。
  →https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers

このサイトにアクセスしてコマンドを実行すれば、大丈夫です。

エラーの原因

エラーが出た際、調べたところ「 1つの実ユーザIDに対して生成できるinotifyのインスタンスの数の上限が決まっており、その上限に達してしまった為 」だそうです。

※inotifyとはLinuxファイルやディレクトリのイベントを監視する機能です。

1つの実ユーザIDに対して生成できる最大ユーザインスタンスを確認してみましょう。

ユーザインスタンスを確認
$ cat /proc/sys/fs/inotify/max_user_instances
1024

というのが出てきます。

inotifyのインスタンス上限を変更

原因が分かったので、inotifyのインスタンスの上限を変更しましょう。

1. inotifyインスタンスの数の上限を524288に変更します。
 2. /etc/sysctl.confにその設定を追記します。
  ※ 「/etc/sysctl.conf」とは、カーネルパラメータを記述する設定ファイルのことです。

/etc/sysctl.confに追記
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf

パラメータの設定を反映

1.「sysctl」コマンドというのを使い、先ほど追記したパラメータを読み込みます。
※1. sysctlとは、ディレクトリ /proc/sys/配下のファイルに書かれたカーネルパラメータを修正するのに利用します。
※2. オプションで特定のファイルを指定しない場合は、「-p」を使い/etc/sysctl.confを読み込みます。

カーネルパラメータを変更
$ sudo sysctl -p

modelを作り直してみよう

先程のmodelを作成
$ rails g model Hogehoge hoge:hoge hoge:hoge
vagrant
Running via Spring preloader in process 2877
      invoke  active_record
      create    db/migrate/20210714052813_create_hoge.rb
      create    app/models/hoge.rb
      invoke    test_unit
      create      test/models/hoge_test.rb
      create      test/fixtures/hoge.yml

できました!

まとめ

notifyのインスタンスの数の上限に達してしまったことにより、modelが作れなくなるエラーの解決を残しました。
初投稿なので見づらいかも知れませんが、暖かい目で見てください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0