'inotifywait running daemon mode giving error Couldn't initialize inotify

I am running inotifywait (inotify-tools-3.14-1) in the daemon mode, however, it gave the following error and no watches are established. Also, since it is within the while loop, there were many inotifywait daemon processes created.

I have no such problem if running with --monitor instead of --daemon. Can someone help fixing it? Thanks a lot.

"Couldn't initialize inotify. Are you running Linux 2.6.13 or later, and was the CONFIG_INOTIFY option enabled when your kernel was compiled? If so, something mysterious has gone wrong. Please e-mail [email protected] and mention that you saw this message."

Below is the code:

while true  # run indefinitely
do
  inotifywait --daemon --outfile /tmp/daemon.log --event close_write --format '%w%f %e %T' --timefmt '%F %T' $folder |  while read eventInfo
     do
        call_another_fun $eventInfo
        break
     done
   done


Solution 1:[1]

You should increase the maximum amount of inotify instances.

sudo sysctl fs.inotify.max_user_instances=2048

On my Desktop System the default of 128 Instances was to low for a few file-browsers, IDEs and electron apps. All of them have multiple inotify instances.

To make this permanent, add this line into /etc/sysctl.conf

fs.inotify.max_user_instances=2048

Solution 2:[2]

Uninstalling

apt-get remove inotify-tools

Then reinstalling

apt-get install inotify-tools

resolved it for me. Between uninstalling and reinstalling I happened to have also run sudo apt autoremove but I doubt that it was part of the solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Weber
Solution 2 David