Watchman – A File and Directory Watching Tool for Changes

[‘

n

Watchman is an open source and cross-platform file watching service that watches files and records or performs actions when they change. It is developed by Facebook and runs on Linux, OS X, FreeBSD, and Solaris. It runs in a client-server model and employs the inotify utility of the Linux kernel to provide a more powerful notification.

n

Useful Concepts of Watchman

n

    n

  • It recursively watches watch one or more directory trees.
  • n

  • Each watched directory is called a root.
  • n

  • It can be configured via the command-line or a configuration file written in JSON format.
  • n

  • It records changes to log files.
  • n

  • Supports subscription to file changes that occur in a root.
  • n

  • Allows you to query a root for file changes since you last checked, or the current state of the tree.
  • n

  • It can watch an entire project.
  • n

n

In this article, we will explain how to install and use watchman to watch (monitor) files and record when they change in Linux. We will also briefly demonstrate how to watch a directory and invoke a script when it changes.

n

Installing Watchman File Watching Service in Linux

n

We will install watchman service from sources, so first install these required dependencies libssl-dev, autoconf, automake libtool, setuptools, python-devel and libfolly using following command on your Linux distribution.

n

----------- On Debian/Ubuntu ----------- rn$ sudo apt install autoconf automake build-essential python-setuptools python-dev libssl-dev libtool rnrn----------- On RHEL/CentOS -----------rn# yum install autoconf automake python-setuptools python-devel openssl-devel libssl-devel libtool rn# yum groupinstall 'Development Tools' rnrn----------- On Fedora -----------rn$ sudo dnf install autoconf automake python-setuptools openssl-devel libssl-devel libtool rn$ sudo dnf groupinstall 'Development Tools'  rn

n

Once required dependencies installed, you can start building watchman by downloading its github repository, move into the local repository, configure, build and install it using following commands.

n

$ git clone https://github.com/facebook/watchman.gitrn$ cd watchmanrn$ git checkout v4.9.0  rn$ ./autogen.shrn$ ./configurern$ makern$ sudo make installrn

n

Watching Files and Directories with Watchman in Linux

n

Watchman can be configured in two ways: (1) via the command-line while the daemon is running in background or (2) via a configuration file written in JSON format.

n

To watch a directory (e.g ~/bin) for changes, run the following command.

n

$ watchman watch ~/bin/rn

n

Watch a Directory in Linux
Watch a Directory in Linux

n

The following command writes a configuration file called state under /usr/local/var/run/watchman/<username>-state/, in JSON format as well as a log file called log in the same location.

n

You can view the two files using the cat command as show.

n

$ cat /usr/local/var/run/watchman/aaronkilik-state/statern$ cat /usr/local/var/run/watchman/aaronkilik-state/logrn

n

You can also define what action to trigger when a directory being watched for changes. For example in the following command, ‘test-trigger‘ is the name of the trigger and ~bin/pav.sh is the script that will be invoked when changes are detected in the directory being monitored.

n

For test purposes, the pav.sh script simply creates a file with a timestamp (i.e file.$time.txt) within the same directory where the script is stored.

n

time=`date +%Y-%m-%d.%H:%M:%S`rntouch file.$time.txtrn

n

Save the file and make the script executable as shown.

n

$ chmod +x ~/bin/pav.shrn

n

To launch the trigger, run the following command.

n

$ watchman -- trigger ~/bin 'test-trigger' -- ~/bin/pav.shrn

n

Create a Trigger on Directory
Create a Trigger on Directory

n

When you execute watchman to keep an eye on a directory, its added to the watch list and to view it, run the following command.

n

$ watchman watch-list rn

n

View Watch List
View Watch List

n

To view the trigger list for a root, run the following command (replace ~/bin with the root name).

n

$ watchman trigger-list ~/binrn

n

Show Trigger List for a Root
Show Trigger List for a Root

n

Based on the above configuration, each time the ~/bin directory changes, a file such as file.2019-03-13.23:14:17.txt is created inside it and you can view them using ls command.

n

$ lsrn

n

Test Watchman Configuration
Test Watchman Configuration

n

Uninstalling Watchman Service in Linux

n

If you want to uninstall watchman, move into the source directory and run the following commands:

n

$ sudo make uninstallrn$ cd '/usr/local/bin' && rm -f watchman rn$ cd '/usr/local/share/doc/watchman-4.9.0 ' && rm -f README.markdown rn

n

For more information, visit the Watchman Github repository: https://github.com/facebook/watchman.

n

You might also like to read these following related articles.

n

    n

  1. Swatchdog – Simple Log File Watcher in Real-Time in Linux
  2. n

  3. 4 Ways to Watch or Monitor Log Files in Real Time
  4. n

  5. fswatch – Monitors Files and Directory Changes in Linux
  6. n

  7. Pyintify – Monitor Filesystem Changes in Real Time in Linux
  8. n

  9. Inav – Watch Apache Logs in Real Time in Linux
  10. n

n

Watchman is an open source file watching service that watches files and records, or triggers actions, when they change. Use the feedback form below to ask questions or share your thoughts with us.

n

‘]