fastest way to monitor a dir

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a perl script running in production which takes a file name
to be scanned continuously and call a user exit shell program
to take action when a string it is looking for is found in
the file. All I do is to sleep for few seconds and wake
and check the size of the file and if it has changed
since last scanned, scan the new lines added since last

Now the specs has changed. We recently found that
some server process don't write to the same file,
but create a new file (usually timestamped) when
some error condition happens. So now my script
also has to change accordingly. From just monitoring
a file constantly, it also has to monitor the dir to
see whether a new file has been dropped and then start
monitoring it (also stop the earlier file it was monitoring).

So my question is, what is the fastest way to check whether
a new file has arrived in a directory. The new file name
will always have a fixed prefix, with a changing suffix
every time (usually timestamp).

Thanks in advance.

Re: fastest way to monitor a dir writes:

Quoted text here. Click to load it

The only portable solution is just to reread the entire directory as
often as needed. Some operation systems have more effective ways to
monitor the filesystem.

On Linux you can use either Linux::Inotify or Linux::Inotify2 (I don't
know the difference) on other unixy system Sys::Gamin seems to be a
possibility. This is based on Gnome's File Alteration Monitor Gamin,
which implements a generic monitor but uses more effective solutions
if available (like inotify on linux).


Re: fastest way to monitor a dir wrote:
Quoted text here. Click to load it

That totally and completely depends upon what features your OS offers
for monitoring directories.


Re: fastest way to monitor a dir wrote:

Quoted text here. Click to load it

What operating system are you using this for?  That will probably
determine the best solution over another.
Tim Greer, CEO/Founder/CTO,, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Re: fastest way to monitor a dir

Quoted text here. Click to load it


thanks all for the replies.

Site Timeline