# file monitoring without polling



## lokli (Oct 23, 2009)

Is there a way to monitor my filesystem without polling every second?
I'am lookling for a way to watch for new files under some directory and watch for file changes, and it should be a real-time solution.

It seems to me, that kqueue is the key to this, but I haven't found a program in ports that could do file monitoring this way.

Also a related question on tail. When tail is invoked like this 
	
	



```
tail -f some_file
```
 is it polling the file or is it using some other mechanism?


----------



## gordon@ (Oct 23, 2009)

tail uses kqueue to do the job. Take a look at the code in usr.bin/tail/forward.c. Specifically look at the code in the follow and set_events subroutines.


----------



## lokli (Oct 23, 2009)

thanks, it seems tail is what i really need.
i'am writing something like a wrapper to irc/ii for personal use

so, i'am trying to make tail read the "out" files

```
tail -F some_file [another_file]
```
this command cliams to show the file, even if it doesn't exist (it shows after creation) , quite a nice feature. But i can't find a way to use wildcards with it, like 

```
tail -F */out
```

am I doning something wrong?
is there a way to add new files to a running instance of tail?

P.S. i'am still interested in directory monitoring using kqueue


----------



## gordon@ (Oct 31, 2009)

The problem is likely due to shell globbing. Assuming that */out doesn't match any files, the shell will pass it as is to the tail process. As a result, tail is attempting to open a file named "*/out" which is unlikely to work. I suppose an enhancement could be made to tail to do the globbing, but I would be concerned about the performance impact of such a change. I also don't know if kqueue/kevent would work with globs (I somehow doubt it).


----------



## lokli (Nov 1, 2009)

"tail -F */out" find files, that already present. 

my point is that i want to monitor files, that are not present yet.

for example i can issue 
	
	



```
tail -F some_channel/out
```
while there is no file "some_channel/out". 



> If the file being followed does not (yet) exist or if it is
> removed, tail will keep looking and will display the file from
> the beginning if and when it is created.



but these doesn't work with "*/out"

i haven't found a way to look for changes in real time, so i just look for them every 10 seconds via shell script.


----------



## gordon@ (Nov 1, 2009)

lokli said:
			
		

> "tail -F */out" find files, that already present.
> 
> my point is that i want to monitor files, that are not present yet.



Right, this is to be expected. When you put tail -F */out on the commandline, the shell expands the */out so the actual command being executed is more like:


```
tail -F foo/out bar/out
```

As a result, suppose at sometime in the future baz/out is created. Since the actual running command doesn't list baz/out in the arg list, it's not going to be detected by the currently running command.

To see what I'm talking about, try running tail -F */out and then run ps in another window. You should see the arguments expanded instead of */out.


----------



## melifaro (Nov 8, 2009)

You can use sysutils/wait_on for monitoring directories/files.


----------

