7

I use inotifywait for event trigger which put file.

When many files are watched by inotifywait, when max_user_watches is exceeded, the following error occurs.

Terminating since out of inotify watches.#012Consider increasing /proc/sys/fs/inotify/max_user_watches

It is necessary to tune /proc/sys/fs/inotify/max_user_watches, but is it possible to check the current file watch number?

Is there a way to check like file-nr in file descriptor?

  • 2
    Shouldn't you award an answer by now? – oligofren Jun 8 '19 at 10:45
12

I cobbled together this little script based on @mosvy's answer.

$ inotify-consumers 

   INOTIFY
   WATCHER
    COUNT     PID     CMD
----------------------------------------
    7111     3079  node /home/myuser/dev/nicecorp-frontend/node_modules/.bin/webpack --config config/webpack.dev.js
      79     1541  /usr/lib/gnome-settings-daemon/gsd-xsettings
      30     1664  /usr/lib/gvfs/gvfsd-trash --spawner :1.22 /org/gtk/gvfs/exec_spaw/0
      14     1630  /usr/bin/gnome-software --gapplication-service
      14     1191  /lib/systemd/systemd --user
      10     1371  /usr/bin/gnome-shell
       9     2459  update-notifier
       7     1635  nautilus-desktop
       5     1191  /lib/systemd/systemd --user
       2     1536  /usr/lib/gnome-settings-daemon/gsd-sound
       2     1452  /usr/lib/evolution/evolution-source-registry
       2     1429  ibus-daemon --xim --panel disable
       2     1248  /usr/bin/dbus-daemon --session --address=systemd: --nofork 
  • 1
    How about the total number? – Exterminator13 Jun 8 '19 at 8:54
  • Which total number? The total number of watchers? Just change the script to sum the counts. – oligofren Jun 8 '19 at 10:43
  • Got a PR in that added the total number! github.com/fatso83/dotfiles/pull/3 – oligofren Aug 28 '20 at 11:31
  • Used this script third time, so adding this for future convenience: curl https://raw.githubusercontent.com/fatso83/dotfiles/master/utils/scripts/inotify-consumers | sh – Dmitry Lukichev Nov 2 '20 at 18:51
7

I wish I was able to answer with something smarter, but the only way I found to count the inotify(7) watches set by a user is this:

find /proc/*/fd -user "$USER" -lname anon_inode:inotify \
   -printf '%hinfo/%f\n' 2>/dev/null |
   xargs cat | grep -c '^inotify'

Change "$USER" with the actual user you want info about; "`id -u`" for the current user. Notice that the max_user_watches is a per-user limit, not per process, or per inotify instance / file descriptor.

This will first find all open file descriptors created by inotify_init*(2), and will then look into the corresponding /proc/PID/fdinfo/FD file for the info about the watch descriptors added with inotify_add_watch(2) to each of them (look into the proc(5) manpage under /proc/[pid]/fdinfo/ for a description of the inotify-specific entries). The -c option of grep will let it print a count of all lines that match a pattern.

Note: the counter for inotify_watches is kept just like other per-user counters in a struct ucounts referenced from the user namespace structure -- see include/linux/user_namespace.h; but I wasn't able to find any interface exposing those counters to userland.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy