File watches & Dropbox
When you stop and start Dropbox through the Dropbox-CLI with dropbox stop; dropbox start
, you might get across a message like
Unable to monitor entire Dropbox folder hierarchy. Please run "echo fs.inotify.max_user_watches=100000 | sudo tee -a /etc/sysctl.conf; sudo sysctl -p" and restart Dropbox to fix the problem.
However, this number of 100.000 file watches isn't accurate. And what does this exactly do?
Spoiler alert: Increasing the number of file watches, is probably not the solution to the syncing problems that I so often have when installing Dropbox on new devices.
File watches
Dropbox uses file watches to keep your cloud storage synchronized with the files on your local machine. Here’s a basic overview of how Dropbox and similar file synchronization services use file watches:
- Monitoring Changes: Dropbox sets up file watches on the directories and files within your Dropbox folder. This allows Dropbox to detect when files or directories are created, modified, deleted, or renamed
- Real-Time Updates: When Dropbox detects a change via file watches, it can immediately process the event. For instance, if you modify a file in your Dropbox folder, Dropbox will detect this change and begin the process of uploading the updated file to its servers
- Synchronizing Across Devices: Dropbox also uses file watches to ensure that changes made on one device are reflected on other devices. When a file is updated on one device, Dropbox watches the change and synchronizes it with the cloud. The cloud then updates the file on all other devices where Dropbox is installed, ensuring consistency
- Efficient Resource Usage: By using file watches, Dropbox can avoid the need for constant scanning of files and directories. Instead of periodically checking all files for changes (which can be resource-intensive), Dropbox reacts to specific events as they occur, making the synchronization process more efficient
- Handling Conflicts and Errors: Dropbox monitors changes to handle conflicts (e.g., when a file is edited on two devices simultaneously) and errors (e.g., if a file cannot be uploaded). This helps maintain file integrity and ensure that the most recent changes are accurately reflected across all devices.
How many file watches?
The number of file watches that Dropbox (or any similar application) should be allowed to have is generally tied to the number of files and directories it needs to monitor. Here’s a more detailed breakdown:
- Files and Directories: Dropbox needs to set up a file watch for each file and directory it monitors. This includes not just the individual files but also directories that may contain other files and subdirectories. Therefore, the total number of file watches required is the sum of all files and directories within the Dropbox folder, including nested directories
- Number of File Watches: The maximum number of file watches you should allow depends on your use case and the total number of files and directories in your Dropbox folder. For practical purposes, it’s often useful to set a limit that is comfortably above the current number of files and directories you expect to monitor. This helps to accommodate future growth without hitting the limit
- Considerations for Limits: On Linux systems, the default limit for file watches might be much lower than what a large Dropbox folder would require. If you have a large number of files and directories, you might need to increase the limit. For example, you might set the limit to a number like 100,000 or even higher, depending on your needs
Calculate the number
It's quite simple to know how many file watches you need - Case 2024.09.05:
cd ~/Dropbox find -type f | wc -l # 1486879 find -type d | wc -l # 119338
- These two numbers together: 1.606.217 watches. As more files are likely to be added to Dropbox in the future, let's increase this to 10 million.
- In the past, I also ended up using a figure of 10 million, so that was quite accurate ['Syncing' maar er gebeurt niets (Dropbox)]
Updating the command
The corresponding command to change this setting to 2 million:
echo fs.inotify.max_user_watches=10000000 | sudo tee -a /etc/sysctl.conf; sudo sysctl -p
The command explained
What it does
echo fs.inotify.max_user_watches=10000000
: Generate the textfs.inotify.max_user_watches=10000000
, which specifies a configuration setting.fs.inotify.max_user_watches
is a parameter that controls the maximum number of file watches a user can create using the inotify system. Setting it to 10000000 increases this limit| sudo tee -a /etc/sysctl.conf
: The pipe|
takes the output from the echo command and sends it as input to the tee command.tee -a /etc/sysctl.conf
appends this output to the/etc/sysctl.conf
file. The/etc/sysctl.conf
file is used to configure kernel parameters at runtime.sudo
is used to runtee
with superuser privileges because modifying/etc/sysctl.conf
requires elevated permissions; sudo sysctl -p
: The semicolon;
separates commands and indicates that the next command should be run after the previous one.sudo sysctl -p
reloads the settings from/etc/sysctl.conf
, applying the newfs.inotify.max_user_watches
value.
Let's check
With
cat /etc/sysctl.conf
you can verify that a line fs.inotify.max_user_watches=10000000
has been added to this file.