首页 > 其他分享 >How to fix: “inotify cannot be used, reverting to polling: Too many open files”

How to fix: “inotify cannot be used, reverting to polling: Too many open files”

时间:2024-04-02 10:11:07浏览次数:206  
标签:files used grep inotify max watches tail user

You are here because you had encountered the error in the title. In this article I will explain the error and steps necessary to reproduce and fix it.

The most relevant documentation for inotify is the inotify man page, that you can also read locally with:

$ man inotify

inotify has three parameters, that can be set in /proc interfaces: `max_user_instances`, `max_user_watches` and `max_queued_events`. We will look into each of them separately.

$ cat /proc/sys/fs/inotify/max_user_instances
128

This specifies an upper limit on the number of inotify instances that can be created per real user ID. inotify instance is created by inotify_init (2) call and returns a file descriptor referring to the inotify instance. Usually each process (e.g. tail) creates one inotify instance. There is an example down below.

$ cat /proc/sys/fs/inotify/max_user_watches
8192

This specifies an upper limit on the number of watches that can be created per real user ID. Each “watch” in the watch list specifies the pathname of a file or directory, along with some set of events that the kernel should monitor for the file referred to by that pathname. One inotify instance can have several inotify watches, which points to different directories/files.

$ cat /proc/sys/fs/inotify/max_queued_events 
16384

`max_queued_events` is an upper limit on the number of events that can be queued to the corresponding inotify instance. As one inotify instance can have several “watches” (i.e. watching potentially large amount of directories/files), not all of the events (like file change, new file etc.) can be processed immediately. In this case, the events will populate an event queue, which has limit of 16384 events by default. the event queue can overflow. In this case, events are dropped (lost).

Now, I demonstrate how max_user_instances and max_user_watches work together. For the demo I will use the following commands:

$ export ILIMIT=128
$ for((i=0;i<=$ILIMIT;i+=1)); do echo $i >> $i.tmp; done
$ for((i=0;i<=$ILIMIT;i+=1)); do bash -c “tail -f $i.tmp &”; done
$ ps aux | grep -v grep | grep tail | awk ‘{print $2}’ | xargs kill -9

The first one is simply exports ILIMIT variable to your current shell and sets its’ value to 128. The second command creates ($ILIMIT+1) files in your current working directory with names 0.tmp, 1.tmp, 2.tmp etc. The third command starts tail -f ($ILIMIT + 1) times on all the files created by the previous command. Finally the last command look for all the tail processes and kills them.

Now, try to execute the commands with the $ILIMIT set to 128:

cd /tmp
export ILIMIT=128
for((i=0;i<=$ILIMIT;i+=1)); do echo $i >> $i.tmp; done
for((i=0;i<=$ILIMIT;i+=1)); do bash -c “tail -f $i.tmp &”; done

On Ubuntu 18.04 I hit the limit on 86th instance:

tail: inotify cannot be used, reverting to polling: Too many open files

I will now increase max_user_instances to set it higher than max_user_watches:

sudo sysctl -w fs.inotify.max_user_instances=10000
$ cat /proc/sys/fs/inotify/max_user_instances 
10000

Clean up your previous tail commands:

ps aux | grep -v grep | grep tail | awk ‘{print $2}’ | xargs kill -9

Now try to create 8500 tail processes, which is lower than default max_user_watches value:

cd /tmp
export ILIMIT=8500
for((i=0;i<=$ILIMIT;i+=1)); do echo $i >> $i.tmp; done
for((i=0;i<=$ILIMIT;i+=1)); do bash -c “tail -f $i.tmp &”; done

I hit the limit on 7996 and have another inotify error message now:

tail: inotify resources exhausted
tail: inotify cannot be used, reverting to polling

Now let’s increase the max_user_watches and keep it in the same proportion as in default settings. Each inotify instance should have 64 watches available:

sudo sysctl -w fs.inotify.max_user_watches=640000
$ cat /proc/sys/fs/inotify/max_user_watches 
640000

Clean up tail processes spawned previously:

ps aux | grep -v grep | grep tail | awk ‘{print $2}’ | xargs kill -9

Now let’s try it again:

cd /tmp
export ILIMIT=8500
for((i=0;i<=$ILIMIT;i+=1)); do echo $i >> $i.tmp; done
for((i=0;i<=$ILIMIT;i+=1)); do bash -c “tail -f $i.tmp &”; done

And now it has been successfully executed! To make a permanent change to the max_user_instances and max_user_watches variable, simply append these lines to /etc/sysctl.conf:

fs.inotify.max_user_instances=10000
fs.inotify.max_user_watches=640000

This will persist the changes after reboot of the machine. To apply the changes immediately, you need to reload the file:

$ sudo sysctl -p

I hope it clarifies a bit why this error happens and how to fix it.

标签:files,used,grep,inotify,max,watches,tail,user
From: https://www.cnblogs.com/zhanchenjin/p/18109982

相关文章

  • C++ | filesystem file not found
    1.filesystem使用过程中遇到的错误如果你用的GCC版本<GCC8.0,则直接#include<filesystem>会报错,说找不到文件Nosuchfileordirectory对于这种情况,可以如下操作。如果是GCC8.0+和C++17以上,则只需要#include<filesystem>①改include路径#include<experimental/filesystem......
  • Ubuntu20.04如何永久修改同一时间打开文件数上限以及解决Too many open files问题
       近期遇到一个问题,写的代码同一时间维护的tcp链接过多,导致linux的文件句柄达到上限,出现Toomanyopenfiles的问题。网上大多回答混乱,在这里做个总结,提醒日后使用。1.查看命令ulimit-a2.临时的修改,关闭终端失效ulimit-n204800或ulimit-SHn204800  //S代......
  • Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field p
    完整日志:Causedby:java.lang.reflect.InaccessibleObjectException:Unabletomakefieldprivatefinaljava.lang.Classjava.lang.invoke.SerializedLambda.capturingClassaccessible:modulejava.basedoesnot"opensjava.lang.invoke"tounnamedmodule......
  • forfiles /?
    forfiles命令是Windows操作系统中的一个命令行工具,用于在指定的文件集合上执行特定的操作。它允许用户按照文件名、文件夹、文件大小、修改日期等条件来筛选文件,并对符合条件的文件执行用户指定的命令。通常情况下,forfiles命令常用于批处理文件(.bat或.cmd文件)中,用于自动......
  • Upload Files
    UploadFiles的时候文件超过64k是会以文件的形式存到windows的temp文件夹的  当iis没有temp文件夹的写入权限上传就全失败了,程序都捕获不到错误  用form-data上传才会有的问题,用文件流的话应该就没有这个问题了 单个文件小于64k的话是......
  • 解决 TS7053: Element implicitly has an any type because expression of type strin
    背景有个接口interfaceDataType{id:number;name:string;created_at:string;updated_at:string;}我的数据{"id":9,"created_at":"2024-03-11T17:50:16.129235+08:00","updated_at":"202......
  • 当使用git出现提示untracked files时怎么办?
    当使用git出现提示untrackedfiles时怎么办?背景介绍:在使用git工具时,遇到如下错误。报错内容:$gitstatusOnbranchmasterNocommitsyetUntrackedfiles:(use"gitadd..."toincludeinwhatwillbecommitted)docs/nothingaddedtocommitbutuntrac......
  • C# 文件监视 FileSystemWatcher
    //官方帮助Console:https://learn.microsoft.com/zh-cn/dotnet/api/system.io.filesystemwatcher?view=net-8.0//官方帮助WPF:https://learn.microsoft.com/zh-cn/dotnet/fundamentals/runtime-libraries/system-io-filesystemwatcher/*FileSysytemWatcher类介绍用......
  • C# 数据流 FileStream
    //StreamMSHelpManual:https://learn.microsoft.com/zh-cn/dotnet/api/system.io.stream?view=net-8.0//FileStream官方手册:https://learn.microsoft.com/zh-cn/dotnet/api/system.io.filestream?view=net-8.0//StreamWriter:https://learn.microsoft.com/zh-cn/dotne......
  • eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0 解释
    eslintsrc--extts,tsx--report-unused-disable-directives--max-warnings0解释一下这段命令这段命令是用来运行ESLint工具检查代码的,针对的是src目录下所有.ts和.tsx后缀的TypeScript文件。命令各部分的具体含义如下:eslint:这是执行ESLint工具本身的命令......