9

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?

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

I cobbled together this little script based on @mosvy's answer. Since the initial conception, it has since seen quite a few improvements (stability on older systems, total count, speed). On most normal machines, running it should take a less than 100 ms. Even though it was much faster than the other alternatives when I made it initially, Simon Matter added some speed enhancements for heavily loaded Big Iron Linux (hundreds of cores) that sped it up immensely, taking it down from ten minutes (!) to 15 seconds.

   INOTIFY
   WATCHER
    COUNT     PID USER     COMMAND
--------------------------------------
    3044   3933 myuser node /usr/local/bin/tsserver
    2965   3941 myuser /usr/local/bin/node /home/myuser/.config/coc/extensions/node_modules/coc-tsserver/bin/tsserverForkStart /hom
     979   3954 myuser /usr/local/bin/node /home/myuser/.config/coc/extensions/node_modules/coc-tsserver/node_modules/typescript/li
       1   3899 myuser /usr/local/bin/node --no-warnings /home/myuser/dev/dotfiles/common-setup/vim/dotvim/plugged/coc.nvim/build/i

    6989  WATCHERS TOTAL COUNT
4
  • 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
8

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.

1
  • By the way, thanks for posting this answer. I wrapped the concept (a bit modified) into a script that became quite useful for a lot of folks. Sharing is caring :) – oligofren Feb 24 at 13:11

Your Answer

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