inotify watch 耗尽

    查看最大 inotify watch 数:

    1. #!/usr/bin/env bash
    2. #
    3. # Copyright 2019 (c) roc
    4. #
    5. # This script shows processes holding the inotify fd, alone with HOW MANY directories each inotify fd watches(0 will be ignored).
    6. result="EXE PID FD-INFO INOTIFY-WATCHES\n"
    7. while read pid fd; do \
    8. exe="$(readlink -f /proc/$pid/exe || echo n/a)"; \
    9. fdinfo="/proc/$pid/fdinfo/$fd" ; \
    10. count="$(grep -c inotify "$fdinfo" || true)"; \
    11. total=$((total+count)); \
    12. result+="$exe $pid $fdinfo $count\n"; \
    13. fi
    14. done <<< "$(lsof +c 0 -n -P -u root|awk '/inotify$/ { gsub(/[urw]$/,"",$4); print $2" "$4 }')" && echo "total $total inotify watches" && result="$(echo -e $result|column -t)\n" && echo -e "$result" | head -1 && echo -e "$result" | sed "1d" | sort -k 4rn;

    示例输出:

    临时调整:

    1. sudo sysctl fs.inotify.max_user_watches=524288

    打开 inotify_add_watch 跟踪,进一步 debug inotify watch 耗尽的原因: