66

I don't understand the best way to set fs.inotify.max_user_watches with sysctl. In fact, I don't understand much of what is happening here other than the fact that I need to set the number of files that can be watched by a particular process.

I believe that I can see the max number of users by running this command:

cat /proc/sys/fs/inotify/max_user_watches

My understanding is that some people suggest changing /proc/sys/fs/inotify/max_user_watches by opening /etc/sysctl.conf in an editor and adding this to it:

fs.inotify.max_user_watches=524288

Then run sudo sysctl -p to -- presumably -- process the changes made to the file.

Others suggest running commands like this:

sudo sysctl -w fs.inotify.max_user_instances=1024
sudo sysctl -w fs.inotify.max_user_watches=12288

I know that -w stands for write, but what is being written and where? Is it just that this command changes /proc/.../max_user_watches?

Which of the two approaches outlined above is best? I understand that 524288 and 12288 are different numbers, but I don't understand the difference between the effect of running -p and -w.

2

2 Answers 2

61

sysctl -w writes kernel parameter values to the corresponding keys under /proc/sys:

sudo sysctl -w fs.inotify.max_user_watches=12288

writes 12288 to /proc/sys/fs/inotify/max_user_watches. (It’s not equivalent, it’s exactly that; interested readers can strace it to see for themselves.)

sysctl -p

loads settings from a file, either /etc/sysctl.conf (the default), or whatever file is specified after -p.

The difference between both approaches, beyond the different sources of the parameters and values they write, is that -w only changes the parameters until the next reboot, whereas values stored in /etc/sysctl.conf will be applied again every time the system boots. My usual approach is to use -w to test values, then once I’m sure the new settings are OK, write them to /etc/sysctl.conf or a file under /etc/sysctl.d (usually /etc/sysctl.d/local.conf).

See the sysctl and sysctl.conf manual pages (man sysctl and man sysctl.conf on your system) for details.

8
  • 7
    I ran into an issue when running a Node/NextJS server to serve an app. The error said something about hitting a limit related to file watchers. After poking around on the Internet, I got things working with fs.inotify.max_user_watches=524288. Do you know a downside to making this change that I should be concerned about?
    – David
    May 27, 2020 at 17:37
  • Stephen to be clear, if the first code line works you then add (e.g.) "fs.inotify.max_user_watches=12288" as a line somewhere (anywhere) in /etc/sysctl.conf? Thanks!
    – dez93_2000
    Aug 4, 2020 at 19:17
  • 2
    @dez93_2000 yes, or better yet, /etc/sysctl.d/local.conf. Aug 4, 2020 at 20:56
  • Cheers muchacho.
    – dez93_2000
    Aug 5, 2020 at 2:28
  • 3
    @Noel you could run out of memory, but that’s extremely unlikely (and you’d run into other problems before that). Jun 18, 2021 at 14:07
6
find /proc/*/fd -lname anon_inode:inotify |
   cut -d/ -f3 |
   xargs -I '{}' -- ps --no-headers -o '%p %U %c' -p '{}' |
   uniq -c |
   sort -nr

See the inotify count used.

2
  • This fails on RHEL6.8. Jun 2, 2021 at 12:01
  • This is wonderful, thank you for sharing ... (works on Debian 11) Sep 17, 2022 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .