ubuntu

如何解决ubuntu inotify错误

小樊
40
2025-09-29 17:41:29
栏目: 智能运维

Understanding the inotify Error
The “System limit for number of file watchers reached” or “Too many open files” error occurs when applications using inotify (a Linux kernel subsystem for monitoring file system changes) hit system-enforced limits. inotify is critical for tools like IDEs (e.g., VSCode), build systems (e.g., Webpack), and file managers to detect real-time changes. When the limit is exceeded, these tools fail to monitor files, leading to broken hot-reloading, delayed updates, or complete service failures.

Step 1: Diagnose Current inotify Limits
Before fixing, verify your system’s current inotify configuration using these commands:

High usage of max_user_watches (e.g., close to the default limit) confirms the need to increase the limit.

Step 2: Temporarily Increase the Watch Limit
To quickly resolve the error for the current session, run:

sudo sysctl fs.inotify.max_user_watches=524288
sudo sysctl -p  # Apply the changes immediately

This raises the max_user_watches limit to 524288 (a common safe value for development machines) without requiring a reboot. Use this method if you need an immediate fix but don’t want permanent changes.

Step 3: Permanently Increase the Watch Limit
For a lasting solution, edit the sysctl configuration file:

  1. Open /etc/sysctl.conf in a text editor (with root privileges):
    sudo nano /etc/sysctl.conf
    
  2. Add the following line at the end of the file:
    fs.inotify.max_user_watches=524288
    
  3. Save the file and exit (Ctrl+O, Enter, Ctrl+X in nano).
  4. Apply the changes:
    sudo sysctl -p
    

This ensures the new limit persists after reboots. For distributions like Arch Linux, you may need to place the line in /etc/sysctl.d/40-max-user-watches.conf instead.

Step 4: Adjust Related Limits (Optional)
If you still encounter issues, increase two additional inotify parameters to handle more concurrent operations:

  1. Max user instances (default: 128):
    echo fs.inotify.max_user_instances=1000 | sudo tee -a /etc/sysctl.conf
    sudo sysctl -p
    
  2. Max queued events (default: 16384):
    echo fs.inotify.max_queued_events=32768 | sudo tee -a /etc/sysctl.conf
    sudo sysctl -p
    

These adjustments help prevent event queue overflows and allow more inotify instances (useful for systems running many monitoring processes).

Step 5: Reduce Monitored Files (Application-Specific Fixes)
If increasing limits isn’t feasible (e.g., on memory-constrained systems), reduce the number of files being monitored:

By following these steps, you can resolve inotify errors on Ubuntu and ensure your tools function correctly. Start with temporary fixes to verify the issue, then implement permanent changes for long-term stability.

0
看了该问题的人还看了