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.
Installing watcherd
On Ubuntu 16.04
Download
$ wget https://github.com/devilbox/watcherd/archive/master.zip
Unzip archive
$ unzip master.zip
If you do not have unzip installed execute
$ sudo apt-get install unzip
And copy to /usr/bin/
$ sudo cp watcherd-master/watcherd /usr/bin/
Usage
Now we can execute with --help
flag
$ 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
-v
verbose output-p ~/test
path 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%n
will be substituted with directory name or you can use%p
to 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.