Mathieu Bouchard <matju / cam.org> wrote: > > I'm trying to watch some (log)files for changes. For this purpose, > > non-blocking I/O and the select function look promising, unfortunately, > > I don't get them to work. > > Here's a snippet of my effort > > Do not use Non-blocking I/O. This is not a concept that is useful here. > NBIO is to be used on pure streams, like pipes, sockets, ... many OS don't > support NBIO on regular files (e.g. afaik Linux). Anyway this would only > allow > you to send several requests to the kernel at the same time. Apparently it works on Linux. Whatever "it" is exactly. That is, I can't claim that I'm sure it actually does non-blocking I/O. Here's the script as it has evolved, so far. #! /usr/bin/ruby require 'fcntl' require 'singleton' trap("SIGHUP") { Logwatch.instance.reload() } module Daemon def Daemon.init puts "Daemon.init" pid = fork() puts "PID #{pid}" if (pid != nil) puts "Parent exit" exit(0) end Process.setsid() Dir.chdir("/") File.umask(0) end end class Logwatch include Daemon include Singleton SLEEP_TIME = 0.2 @watchfiles = [] def setupWatchfiles() # needs to read file paths from config file testfile = open("/tmp/messages", "r") testfile.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) testfile.seek(0, IO::SEEK_END) @@watchfiles = [testfile] end def watch() while (true) inputAvail = select(@@watchfiles)[0] inputSucc = false inputAvail.each() do |f| begin s = f.sysread(100); inputSucc = true puts s rescue EOFError end end sleep(SLEEP_TIME) unless inputSucc end puts "Done watching." end def reload() puts "Reloading list of watched files..." @@instance.setupWatchfiles() puts "done." end def run() Daemon.init() setupWatchfiles() watch() end end Logwatch.instance.run() > I don't know whether select() works at all on regular files. At least it looks as if it does. Michael -- Michael Schuerig mailto:schuerig / acm.org http://www.schuerig.de/michael/