Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Watchman Monitor File Changes in Linux?

· 5 min read · Home Tech

In this article
  1. What Is Watchman and Why Use It?
  2. How Watchman Works Under the Hood
  3. Installing Watchman on Linux
  4. Basic Usage: Watching and Triggering
  5. Querying Changes and Using the Clock
  6. Watchman vs. inotify: What’s the Difference?
  7. Common Pitfalls and Troubleshooting
  8. Frequently Asked Questions

Yes, Watchman is a powerful and widely used tool for monitoring file changes in Linux, developed by Meta (Facebook). It watches files and directories, records changes, and can trigger actions or provide a queryable history of what changed. This article explains how Watchman works, how to install and use it, and how it compares to alternatives like inotify.

What Is Watchman and Why Use It?

Watchman is an open-source file-watching service that runs as a daemon on Linux, macOS, and Windows. It was created to handle the scale and complexity of large codebases, especially those at Meta. Unlike simpler tools that watch a single directory, Watchman can watch entire directory trees, detect changes recursively, and maintain a persistent database of file metadata and change history.

Developers use Watchman for tasks like automatically rebuilding projects, running tests when files change, syncing files to remote servers, and triggering live-reload in development servers. Its key advantages are speed, reliability, and the ability to handle very large trees without missing events.

How Watchman Works Under the Hood

Watchman uses the operating system’s native file notification mechanisms—on Linux, that’s inotify—to get real-time events. However, it doesn’t just pass those events through. It maintains its own view of the filesystem, called a watch, and records every change in a persistent store. This allows you to query the history of changes, not just react to the latest one.

When you start Watchman, it does an initial crawl of the watched directory to build a baseline. After that, it listens for inotify events and updates its database. If events are missed (for example, due to a system crash or a temporary unmount), Watchman can perform a recrawl to resync. This makes it more robust than raw inotify, which can lose events if the queue overflows.

Installing Watchman on Linux

Watchman is available in the package repositories of many Linux distributions, but the version may be older. For the latest version, you can build from source or use a package from GitHub releases.

  • Ubuntu/Debian: sudo apt install watchman (may be outdated)
  • Fedora: sudo dnf install watchman (if available)
  • Arch Linux: sudo pacman -S watchman (community repository)
  • From source: Clone the GitHub repository, install dependencies (autoconf, automake, libtool, pkg-config, python3), then run ./autogen.sh, ./configure, make, and sudo make install.

After installation, verify with watchman --version. The daemon starts automatically when you run a watchman command.

Basic Usage: Watching and Triggering

To watch a directory, use the watch command:

watchman watch /path/to/project

This starts the daemon and begins monitoring. To see all watches, use watchman watch-list. To trigger a command when files change, you use the trigger command:

watchman -- trigger /path/to/project build -- npm run build

This will run npm run build whenever any file changes in the project. You can restrict triggers to specific file patterns using the --patterns option, for example:

watchman -- trigger /path/to/project rebuild --patterns '*.js' -- npm run build

To remove a trigger, use watchman trigger-del /path/to/project build. To stop watching entirely, use watchman watch-del /path/to/project.

Querying Changes and Using the Clock

One of Watchman’s most powerful features is the ability to query the change history. You can ask “what changed since a certain time?” and get a list of files. This is done with the query command, using the since parameter with a clock value.

To get the current clock, use watchman clock /path/to/project. Then, later, you can query:

watchman -j <<EOF
["query", "/path/to/project", {"since": "c:12345:67890", "fields": ["name", "exists", "type"]}]
EOF

The response lists files that changed since that clock, along with whether they still exist and their type. This is extremely useful for incremental builds and syncing tools.

Watchman vs. inotify: What’s the Difference?

inotify is the Linux kernel’s low-level file change notification API. Tools like inotifywait (from inotify-tools) provide a simple command-line interface to it. Watchman is built on top of inotify but adds a layer of abstraction.

  • Recursion: inotify requires you to watch each subdirectory separately; Watchman handles recursion automatically.
  • History: inotify only gives you events as they happen; Watchman stores a database you can query.
  • Trigger management: Watchman can manage multiple triggers and restart them after a daemon restart.
  • Portability: Watchman works across OSes, while inotify is Linux-only.

For simple one-off commands, inotifywait might be sufficient. But for complex, long-running projects, Watchman’s features save time and reduce errors.

Common Pitfalls and Troubleshooting

Users often encounter a few issues with Watchman:

  • Watchman daemon not starting: Check that the watchman user and state directory are correctly set. On some systems, you may need to run watchman shutdown-server and restart.
  • High CPU usage: This can happen if you watch a directory with a huge number of files (e.g., node_modules). Use the --exclude option or add a .watchmanconfig file to ignore certain directories.
  • Missed events: If the inotify queue overflows, Watchman may miss events. Increase the system limit with sysctl fs.inotify.max_user_watches.
  • Trigger not firing: Ensure the trigger command is correct and that the daemon has permission to execute it. Check the logs at /usr/local/var/run/watchman or /tmp/watchman.

Adding a .watchmanconfig file in the root of your project can help. For example, to ignore node_modules:

{
  "ignore_dirs": ["node_modules", "dist"]
}

Frequently Asked Questions

Is Watchman free to use?

Yes, Watchman is open-source software released under the MIT license. You can use it freely in personal and commercial projects.

Can Watchman watch multiple directories at once?

Yes, you can run watchman watch on multiple paths. The daemon will handle them all, and you can query each independently.

Does Watchman work on other operating systems?

Yes, Watchman supports macOS (using FSEvents) and Windows (using ReadDirectoryChangesW) in addition to Linux. This makes it a good choice for cross-platform development teams.

How do I stop Watchman from running in the background?

You can shut down the daemon with watchman shutdown-server. This stops all watches and triggers. To restart, simply run any watchman command again.

What is the difference between Watchman and nodemon?

Nodemon is a Node.js tool that restarts your application when files change. Watchman is a general-purpose file watcher that can trigger any command, not just Node.js restarts. Watchman is more flexible and can be used with any language or toolchain.

How does Watchman handle symbolic links?

By default, Watchman does not follow symbolic links to directories to avoid infinite loops. You can configure it to follow them with the follow_symlinks option in .watchmanconfig, but this is generally not recommended.