Inotify in Linux and MacOS

Inotify (inode notify) is a Linux kernel subsystem, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload. The inotifywait and inotifywatch commands allow using the inotify subsystem from the command line.

On Linux:

To check the current config:

1$ sysctl fs.inotify
2fs.inotify.max_queued_events = 16384
3fs.inotify.max_user_instances = 128
4fs.inotify.max_user_watches = 67536

Or

1$ cat /proc/sys/fs/inotify/max_queued_events
216384
3$ cat /proc/sys/fs/inotify/max_user_instances
4128
5$ cat /proc/sys/fs/inotify/max_user_watches
667536

Explains:

  • max_user_instances is the maximum number of watch instances (= number of root dirs for watching).
  • max_user_watches is the maximum number of dirs across all watch instances.
  • max_queued_events the maximum number of events in the kernel queue.

Note: Usually, you should modify the max_user_instances/max_user_watches values and keep max_queued_events value as the default.

On macOS

To check the current config:

1$ sudo sysctl kern | grep maxfile
2kern.maxfiles: 49152
3kern.maxfilesperproc: 24576

To modify the configuration permanently.

On Linux

Open /etc/sysctl.conf, modify these following lines or add new if they do not exist

1fs.inotify.max_queued_events = 16384
2fs.inotify.max_user_instances = 128
3fs.inotify.max_user_watches = 16384

Then run below command, to reload the changes.:

1sudo sysctl -p
On macOS,

Open /etc/sysct.conf and modify these following lines or add new if they do not exist

1kern.maxfiles=49152
2kern.maxfilesperproc=24576

The configuration will be used from the next reboot.

To modify the configuration on running system, which does not affect in the next restart.

On Linux:
1sudo sysctl -n -w fs.inotify.max_queued_events=16384
2sudo sysctl -n -w fs.inotify.max_user_instances=128
3sudo sysctl -n -w fs.inotify.max_user_watches=16384
On macOS:
1sudo sysctl -w kern.maxfiles=49152
2sudo sysctl -w kern.maxfilesperproc=24576

Check which processes are consuming inotify resources

1ps -p $(find /proc/*/fd/* -type l -lname 'anon_inode:inotify' -print 2> /dev/null | sed -e 's/^\/proc\/\([0-9]*\)\/.*/\1/')

List of processes id with the number of inotify instances they are consuming:

1for foo in /proc/*/fd/*; do readlink -f $foo; done | grep inotify | sort | uniq -c | sort -nr

Error usually occurs

1the configured user limit (128) on the number of inotify instances has been reached

I :heart: AWS! :smile: Enjoy