Skip to content

Instantly share code, notes, and snippets.

@saravanabalagi
Created June 1, 2022 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saravanabalagi/486ff7818760388ed653f3cb2a0b1afe to your computer and use it in GitHub Desktop.
Save saravanabalagi/486ff7818760388ed653f3cb2a0b1afe to your computer and use it in GitHub Desktop.
Get the procs sorted by the number of inotify watchers
#!/bin/sh
# Get the procs sorted by the number of inotify watchers
# @author Carl-Erik Kopseng
# @latest https://github.com/fatso83/dotfiles/blob/master/utils/scripts/inotify-consumers
# Discussion leading up to answer: https://unix.stackexchange.com/questions/15509/whos-consuming-my-inotify-resources
usage(){
cat << EOF
Usage: $0 [--help|--limits]
-l, --limits Will print the current related limits and how to change them
-h, --help Show this help
EOF
}
limits(){
echo "\nCurrent limits\n-------------"
sysctl fs.inotify.max_user_instances fs.inotify.max_user_watches
cat <<- EOF
Changing settings permanently
-----------------------------
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
sudo sysctl -p # re-read config
EOF
}
if [ "$1" = "--limits" -o "$1" = "-l" ]; then
limits
exit 0
fi
if [ "$1" = "--help" -o "$1" = "-h" ]; then
usage
exit 0
fi
if [ -n "$1" ]; then
echo "\nUnknown parameter '$1'\n" > /dev/stderr
usage
exit 1
fi
generateRawData(){
# From `man find`:
# %h Leading directories of file's name (all but the last element). If the file name contains no slashes (since it
# is in the current directory) the %h specifier expands to `.'.
# %f File's name with any leading directories removed (only the last element).
#
find /proc/*/fd \
-lname anon_inode:inotify \
-printf '%hinfo/%f\n' 2>/dev/null \
\
| xargs grep -c '^inotify' \
| sort -n -t: -k2 -r
}
printf "\n%10s\n" "INOTIFY"
printf "%10s\n" "WATCHER"
printf "%10s %5s %s\n" " COUNT " "PID" "CMD"
printf -- "----------------------------------------\n"
IFS=''; # to avoid `read` from interpreting whitespace and keep whole lines
total_watchers=0
generateRawData | { while read line; do
watcher_count=$(echo $line | sed -e 's/.*://')
total_watchers=$((total_watchers+watcher_count))
pid=$(echo $line | sed -e 's/\/proc\/\([0-9]*\)\/.*/\1/')
if [ $pid -gt 0 ]; then
cmdline=$(ps --columns 120 -o command --no-headers -p $pid)
fi
printf "%8d %7d %s\n" "$watcher_count" "$pid" "$cmdline"
done
printf "\n%8d %s\n" "$total_watchers" "WATCHERS TOTAL COUNT"
}

Inotify Limits

Often when setting up vscode server, a warning related to inotify limits (number of max user watches) is issued. If you want to check the current limits

sh ./inotify-limit.sh

Instructions to change to limits will be printed after the info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment