1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

file monitoring without polling

Discussion in 'General' started by lokli, Oct 23, 2009.

  1. lokli

    lokli New Member

    Messages:
    5
    Likes Received:
    0
    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:
    tail -f some_file
    is it polling the file or is it using some other mechanism?
     
  2. gordon@

    gordon@ New Member Developer

    Messages:
    404
    Likes Received:
    0
    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.
     
  3. lokli

    lokli New Member

    Messages:
    5
    Likes Received:
    0
    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
    Code:
    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:
    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
     
  4. gordon@

    gordon@ New Member Developer

    Messages:
    404
    Likes Received:
    0
    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).
     
  5. lokli

    lokli New Member

    Messages:
    5
    Likes Received:
    0
    "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:
    tail -F some_channel/out
    while there is no file "some_channel/out".

    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.
     
  6. gordon@

    gordon@ New Member Developer

    Messages:
    404
    Likes Received:
    0
    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:
    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.
     
  7. melifaro

    melifaro New Member

    Messages:
    4
    Likes Received:
    0