In this article, I am going to tell you about shell directory listener called watcherd. Daemon watcherd will check the directory changes and execute specified commands or shell scripts.
On Ubuntu 16.04
$ wget https://github.com/devilbox/watcherd/archive/master.zip
$ unzip master.zip
If you do not have unzip installed execute
$ sudo apt-get install unzip
And copy to
$ sudo cp watcherd-master/watcherd /usr/bin/
Now we can execute with
$ watcherd --help
Let's create a directory called test and make watcherd working
$ mkdir test
Now start watcherd with simple parameters
$ watcherd -v -p ~/test -a "echo added %n" -d "echo deleted %n" -t "ls -l ~/test"
A little hint about watcherd parameters
-p ~/testpath to directory
-a "echo added %n"execute
"echo added"if any directory is created in path
-d "echo deleted %n"execute
"echo deleted"if any directory is deleted in path
%nwill be substituted with directory name or you can use
%pto get a full path to the created or deleted directory
-t "ls -l ~/test"execute command after triggering
&run in background
And check if it is running
$ ps ax | grep watcherd
Now we can try to create a new directory called
aaa in our test folder
$ mkdir test/aaa
As we can see after creating directory
aaa watcherd executed
'echo added aaa', we received watcherd verbose message with adding a new directory and its full path and
ls -l trigger output.
Now let's delete this directory and check the output
$ rm -r test/aaa
We got the same output just with the
echo deleted aaa. But instead of doing
echo you can perform there any shell command or run any script.
This daemon can be very useful with web servers such as nginx or apache for monitoring new folders with websites, creating new configuration files and restarting web server.