UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2009-11-10 11:47 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Detecting new files on directory

Hello everybody,

Do you know if it's possible to detect when some new files are put into a directory? I mean, is it possible to monitor a directory in order to do something when a new file is added, or modified, or whatever in that directory?

What do I want to do? I want to monitor a directory where users can put files via FTP, and whenever a new file arrives, I want to start processing it. I am currently using a cronjob that executes every 5 minutes or so, but that's a lousy solution because our users are transferring the files not that often. However, I cannot use a longer period of time because the users expect their files processed soon enough.

I don't want to waste resources running the cronjob, and I want to give the users a good experience.

Any suggestions are welcomed...

Regards,

HLR

Offline

#2 2009-11-11 03:09 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: Detecting new files on directory

On Linux you can use dnotify: F_NOTIFY with fcntl(2). It's a bit icky, but it
does what you need. With newer (2.6.13) kernels you can also use inotify(7).

An alternative is to use a file monitoring library or daemon like Gamin or FAM,
which is more portable and falls back on polling if no suitable notification
method is available.

Offline

#3 2009-11-11 04:58 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: Detecting new files on directory

i3839;28080 wrote:

On Linux you can use dnotify: F_NOTIFY with fcntl(2). It's a bit icky, but it
does what you need. With newer (2.6.13) kernels you can also use inotify(7).


Hello i3839,

Thank you. I checked but unfortunately inotify is not available on the system, and unfortunately I cannot make any modifications to the system.

dnotify actually works, and it's quite fine for what I want to do.

Offline

Board footer

Powered by FluxBB