Kernel_Killer
March 3rd, 2006, 05:40
I'm trying to figure out a way to have an application react when a certain file comes in onto a system. In otherwords, a file appears in a certain directory, and then the system automatically acts on the file. I'm thinking I might be able to do this with swatch, but if anyone else has any recommendations, please let me know.

oenone
March 3rd, 2006, 12:24
cronjob in which you test if the file is there and then do the action?
that would be the easiest, i think.

regards,
oenone

Strog
March 3rd, 2006, 13:26
There's quite a few apps/daemons out there to do this. There's the whole *notify programs and fam/gamin (py-gamin too if you like). I guess it all depends on exactly what you are up to. :biggrin:

Kernel_Killer
March 4th, 2006, 09:00
Well, the file is going to be sftped to a directory, and the daemon needs to use the values in that file to create a user.

bmw
March 5th, 2006, 12:55
KK, if you are up to a challenge, then check out kevent (in the kqueue(2) manpage). kevent is a kernel API that can notify you when specific events occur, eg: writes to a file, data available in a socket, etc. I think that you could setup an EVFILT_VNODE kevent filter on the directory into which your file will be created, and wait for a NOTE_WRITE event to occur. That would indicate the the dir has been updated, ie a file has been added.

kevent can notify you instantly because your process/thread can sleep waiting for the event and the kernel reschedules your process (wakes it up) immediately that the event occurs. The downside is that you'll have to tinker with this stuff and it's a bit tricky. I can't guarantee that it will work on directories either. I know it works with plain files, but personally I have so far only used kqueue/kevent with sockets.

I use libevent (Niels Provos) to make programming events easier. See the devel/libevent port. This makes your application event-driven and so it won't sleep waiting for the wake-up and therefore other stuff can be going on at the same time.

Cheers!

Kernel_Killer
March 5th, 2006, 13:05
Thanks a ton BMW, I'll give it a shot! Always up for a challenge. :biggrin:

Kernel_Killer
March 9th, 2006, 19:11
Ok. As a final solution, I re-wrote the script in Python, and used py-execnet. Still, looking at some of these for later use, and other uses.