fswatch monitors one or more directories for changes and runs commands when
such changes are detected. This is mostly useful when developing.
-
Imagine that you're writing a Python script to do something fancy. You are adding features and would like to (re)run the script as soon as you save its sourcefile:
cd ~/src fswatch python3 myscript.py
Now
fswatchwill happily monitor the current directory (~/srcthat youcdd into) and runpython3 myscript.pyas soon as something in the the directory changes (in fact also when something in a subdirectory of~/srcchanges). By default,fswatchmonitors the current directory. -
Imagine that
myscript.pyreads some files from~/dataand processes them. You want to rerun it as soon as something changes in the data files:fswatch -d ~/data python3 myscript.py`
Here, flag
-dadds a directory (plus its subdirs) to be watched. -
Or, combined: you want to watch for any changes in your
~/srcdirectory, and for changes in~/data, and rerun the script when anything changes:cd ~/src fswatch -d . -d ~/data python3 myscript.py
-
Imagine that you are developing a program
myprogthat's built usingmake. As soon as you make a change to the sources, you want to re-runmaketo see if all still compiles:fswatch make
-
While developing
myprogyou'd not only like to re-runmakewhen something changes, but you'd also want to startmyprogto see that it still works:fswatch -- sh -c 'make && ./myprog'Here, using
sh -c 'make && ./myprogwill start./myprogonly whenmakesucceeds. (Read up on shell scripting if this is new to you.).fswatchneeds the flag--to tell it to stop scanning for flags, or it would get confused by the hypen in-cwhich isn't forfswatchbut forsh.
There are of course flags to control the behaviour of fswatch. You get a
full listing by running fswatch without arguments (or fswatch -h). Some
of the flags that might need explaining, are:
-
-a: Without this flag,fswatchskips watching typical editor backup files, ones that start with#or~or.. If you wantfswatchto also monitor such files, add a-a. -
-f: Using this flag you can add separate files to the watchlist. Not just just directories can be watched, you can limit to singular files. -
-K: Without this flag,fswatchwill stop when it has no more files to watch. If the watched directory might become empty, butfswatchshould patiently wait for new files, add-K. -
-k NSEC(nr. of seconds): Whenfswatchdetects a change, andNSEChas expired, then it will kill any running program and redo all. This is handy when you are developing daemons that otherwise don't terminate. The default is 30 seconds.
Clone the repository and simply type make. You will need a C++ compiler. This
makes fswatch locally (in the sources directory).
If you want to install the program into $HOME/bin (i.e., your personal bin
directory), then type make install (or make me a sandwich if you are so
inclined).
If you want to install it into another directory, e.g. /usr/local/bin, type
BINDIR=/usr/local/bin make installfswatch is distributed under GPLV3, which basically means that you're free to
do with it as you like, you may modify it, provided that you keep the license
intact and that you make your changes available to everyone. As a personal
favor, I'd like to be informed of any changes or new handy features so that I
can merge them into the sources for a next version.
See the file LICENCE.md.