# Monitoring a directory?



## Floren (Jan 19, 2021)

My goal is to have a service running and monitor if a specific directory is deleted. If it is, recreate it. Pretty much the fuctionality of Linux `inotify`. I'm a FreeBSD newbie, coming from Linux. I looked at tools like:
- https://www.freshports.org/devel/gamin
- https://www.freshports.org/sysutils/watchman

I would like to have a `service` running which will read a config file and act on it, so if I reboot I don't have to worry about it. For example `watchman` package has no service, or I cannot find it. Thank you for your help.


----------



## chrbr (Jan 19, 2021)

Dear Floren,
may be mtree(1) suits your needs. It compares a directory and its sub directories against a spec file which is the reference. The reference is generated as in the example below. The $-something are assembled before.

```
/usr/sbin/mtree -c -K md5 -R time -p $mtree_dir | tee $mtree_spec
```
The comparison is as below.

```
/usr/sbin/mtree -K md5 -R time -p $mtree_dir -f $mtree_spec
```
You can add or exclude tons of options and also exclude file. I have that things in a script to monitor /etc, /usr/local/etc and others run by cron(8). May be you can use that method for your project.


----------



## Floren (Jan 20, 2021)

Thank you chrbr, but this is not what I'm looking for. What I need is running service based on gamin or watchman (alternatives to inotify in FreeBSD) which monitors a specific directory (i.e. /var/tmp/somedir). If the directory is deleted, the service will create instantly, not relying on a cronjob or other repetitive mechanisms. In other words, if the directory is not deleted for 3 years, no action will be performed.

Your solution is good to compare a a tree of dirs and files, but you need to run it through a cronjob which is not useful for me. I might have that directory deleted 30 times in one minute and any job looking for directory existence will fail.

Did anyone used watchman? Is much more compact than gamin, requiring only one library (pcre).


----------



## ta0kira2 (Jan 23, 2021)

If you're comfortable writing C, take a look at `EVFILT_VNODE` with kevent(2). This _is not_ 1:1 equivalent to `inotify`, but it should be close enough here. (IIRC, you _can't_ do things like monitor all files in a directory at one time without a descriptor for each file.)


----------



## Speedy (Jan 23, 2021)

sysutils/daemontools perhaps can be of use?


----------

