Directory Monitoring on a Server?
November 1, 2010 6:23 PM Subscribe
Is there a way to run a PHP script when a file is added to a directory on a server?
I've written a PHP script which parses text messages and processes the results. The text messages show up as text files in a directory. The script deletes the messages when it's done processing them.
So, Metafilter, is there a way to make my script run when files are added to the directory without having the script be running all the time? I would use cron jobs, but that's a minute delay which makes the entire process too long to be usable.
My server is hosted by Bluehost if that makes a difference.
I can rewrite the script in a different language if I have to.
Thanks!
I've written a PHP script which parses text messages and processes the results. The text messages show up as text files in a directory. The script deletes the messages when it's done processing them.
So, Metafilter, is there a way to make my script run when files are added to the directory without having the script be running all the time? I would use cron jobs, but that's a minute delay which makes the entire process too long to be usable.
My server is hosted by Bluehost if that makes a difference.
I can rewrite the script in a different language if I have to.
Thanks!
Response by poster: The files are arriving via email.
Is there a way to make a script run when new emails arrive (on a generic email setup for a server)?
posted by 47triple2 at 6:30 PM on November 1, 2010
Is there a way to make a script run when new emails arrive (on a generic email setup for a server)?
posted by 47triple2 at 6:30 PM on November 1, 2010
Well, on my home Linux server I use incron which is like cron but it responds to filesystem activity rather than time. That link was just the first I came to in Google.
I don't know if your host will have that installed or will be willing to install it...could be dicey from their perspective.
posted by forthright at 6:31 PM on November 1, 2010
I don't know if your host will have that installed or will be willing to install it...could be dicey from their perspective.
posted by forthright at 6:31 PM on November 1, 2010
The file arrives by email? You probably want to use procmail then.
posted by Cat Pie Hurts at 6:38 PM on November 1, 2010 [1 favorite]
posted by Cat Pie Hurts at 6:38 PM on November 1, 2010 [1 favorite]
Procmail! Old, clunky, steep learning curve, but if your mail is the old standard *NIXY local delivery procmail can fire off a process on a message among other things. This little monster pipes all messages coming in that match a specific Subject line through a script:
It depends whether or not your provider is setup for procmail support. This would actually never write the mail to the disk in the first place.
posted by zengargoyle at 6:44 PM on November 1, 2010 [1 favorite]
:0:in-asn-alert.lock
* ^Subject: ASN ALERT.*
| $HOME/bin/asnwatch >>in-asn-alert
It depends whether or not your provider is setup for procmail support. This would actually never write the mail to the disk in the first place.
posted by zengargoyle at 6:44 PM on November 1, 2010 [1 favorite]
Best answer: Bluehost apparently had procmail support enableable from the control panel at least at one point, according to this Bluehost procmail guide.
Procmail syntax is a bit arcane but probably do exactly what you want - receive incoming emails, pick out the ones you want, and send them to a script of your choice. I used it for creating an early mms-to-web gateway for my first cameraphone.
posted by lantius at 6:52 PM on November 1, 2010 [1 favorite]
Procmail syntax is a bit arcane but probably do exactly what you want - receive incoming emails, pick out the ones you want, and send them to a script of your choice. I used it for creating an early mms-to-web gateway for my first cameraphone.
posted by lantius at 6:52 PM on November 1, 2010 [1 favorite]
Nthing procmail, if you have access to the rules. Bluehost may or may not allow it. If you have cPanel, you can pipe it to a program using the Forwarding applet.
posted by Old'n'Busted at 6:54 PM on November 1, 2010
posted by Old'n'Busted at 6:54 PM on November 1, 2010
If that Bluehost procmail guide is correct, you wouldn't even need procmail. If you can filter your incoming messages to match a filter you can pipe them to a program directly.
posted by zengargoyle at 7:23 PM on November 1, 2010
posted by zengargoyle at 7:23 PM on November 1, 2010
If you can't run procmail, you can watch a directory for changes with inotify.
posted by nicwolff at 7:59 PM on November 1, 2010
posted by nicwolff at 7:59 PM on November 1, 2010
Response by poster: Thanks guys! Procmail works perfect!!!
posted by 47triple2 at 8:06 PM on November 1, 2010
posted by 47triple2 at 8:06 PM on November 1, 2010
I have to admit, I'm curious what kind of thing is happening where once a minute isn't fast enough...
posted by AmbroseChapel at 11:08 PM on November 1, 2010
posted by AmbroseChapel at 11:08 PM on November 1, 2010
This thread is closed to new comments.
posted by Old'n'Busted at 6:25 PM on November 1, 2010