file monitoring without polling

General questions about the FreeBSD operating system. Ask here if your question does not fit elsewhere.

file monitoring without polling

Postby lokli » 23 Oct 2009, 02:20

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
Code: Select all
tail -f some_file
is it polling the file or is it using some other mechanism?
lokli
Junior Member
 
Posts: 5
Joined: 23 Oct 2009, 02:02

Postby gordon@ » 23 Oct 2009, 04:53

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.
gordon@
Member
 
Posts: 404
Joined: 20 Feb 2008, 22:44
Location: San Diego, CA

Postby lokli » 23 Oct 2009, 09:05

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

so, i'am trying to make tail read the "out" files
Code: Select all
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
Code: Select all
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
lokli
Junior Member
 
Posts: 5
Joined: 23 Oct 2009, 02:02

Postby gordon@ » 31 Oct 2009, 05:31

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).
gordon@
Member
 
Posts: 404
Joined: 20 Feb 2008, 22:44
Location: San Diego, CA

Postby lokli » 01 Nov 2009, 08:47

"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
Code: Select all
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.
lokli
Junior Member
 
Posts: 5
Joined: 23 Oct 2009, 02:02

Postby gordon@ » 01 Nov 2009, 17:27

lokli wrote:"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:

Code: Select all
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.
gordon@
Member
 
Posts: 404
Joined: 20 Feb 2008, 22:44
Location: San Diego, CA

Postby melifaro » 08 Nov 2009, 15:08

You can use [port]sysutils/wait_on[/port] for monitoring directories/files.
melifaro
Junior Member
 
Posts: 4
Joined: 16 Nov 2008, 18:37


Return to General

Who is online

Users browsing this forum: cuq, kpa, trh411 and 1 guest