From: John Carter Date: 2005-07-21T10:18:56+09:00 Subject: Re: Hotfolders or "watched" folders in Ruby? On Thu, 21 Jul 2005, Keith Fahlgren wrote: > I'd like to have a daemon process that watches for new files in a > specific directory and then runs a command on them once they're there. If you are on Linux, it would be trivial to wrap something around /dev/inotify From /usr/src/linux/Documentation/filesystems/inotify.txt or.. http://www.ibiblio.org/peanut/Kernel-2.6.12/filesystems/inotify.txt inotify a powerful yet simple file change notification system Document started 15 Mar 2005 by Robert Love (i) User Interface Inotify is controlled by a device node, /dev/inotify. If you do not use udev, this device may need to be created manually. First step, open it int dev_fd = open ("/dev/inotify", O_RDONLY); Change events are managed by "watches". A watch is an (object,mask) pair where the object is a file or directory and the mask is a bitmask of one or more inotify events that the application wishes to receive. See for valid events. A watch is referenced by a watch descriptor, or wd. Watches are added via a file descriptor. Watches on a directory will return events on any files inside of the directory. Adding a watch is simple, /* 'wd' represents the watch on fd with mask */ struct inotify_request req = { fd, mask }; int wd = ioctl (dev_fd, INOTIFY_WATCH, &req); You can add a large number of files via something like for each file to watch { struct inotify_request req; int file_fd; file_fd = open (file, O_RDONLY); if (fd < 0) { perror ("open"); break; } req.fd = file_fd; req.mask = mask; wd = ioctl (dev_fd, INOTIFY_WATCH, &req); close (fd); } John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Carter's Clarification of Murphy's Law. "Things only ever go right so that they may go more spectacularly wrong later." From this principle, all of life and physics may be deduced.