how do you watch for an incoming file

Conor P. Cahill cpcahil at virtech.uucp
Wed Nov 29 15:29:55 AEST 1989


In article <5506 at hplabsb.HP.COM>, quan at hplabsb.HP.COM (Suu Quan) writes:
> 	How do I write a C-program or shell script that will watch for
> the presence of a new file in a certain directory.

   [example shell deleted]
> 	But I would prefer a C-program or a script using a blocking command

There is no way to sleep on a file being added to a directory.  If you can't
modify the software that is sending the file (to use some kind of IPC to 
tell your daemon that the file is there), the next best thing you could
do is:

	set dir mtime var to non zero;
	set old mtime var to zero;
	forever loop
	{
		if( dir mtime var != old mtime var)
		{
			/* The dir has been modified, so look for new stuff...*/
			old mtime var = dir mtime var
		}
		else
		{
			sleep(however long is ok);
		}
		stat(dir,to get current dir mtime var)
	}

NOTE-> the code above was just off the top of my head, so there may be a typo,
       bug, mistake, or other such stuff.

This way you are only stating the directory every x seconds (unless there
is a file to process) and this shouldn't use up any measurable CPU as long
as long as the sleep is > 1 second.

-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+



More information about the Comp.unix.questions mailing list